Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  38] [ 7]  / answers: 1 / hits: 148747  / 12 Years ago, wed, january 23, 2013, 12:00:00

I would like to get the starting position of the 2nd occurrence of ABC with something like this:



var string = XYZ 123 ABC 456 ABC 789 ABC;
getPosition(string, 'ABC', 2) // --> 16


How would you do it?


More From » javascript

 Answers
7



const string = XYZ 123 ABC 456 ABC 789 ABC;

function getPosition(string, subString, index) {
return string.split(subString, index).join(subString).length;
}

console.log(
getPosition(string, 'ABC', 2) // --> 16
)




[#80680] Tuesday, January 22, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monica

Total Points: 308
Total Questions: 102
Total Answers: 109

Location: Saudi Arabia
Member since Sat, Aug 20, 2022
2 Years ago
;