I want to get the index of First ever character appear in the string.
for example : I have a string " 225 get first character " In this I want the index of 'g' .how to get this ?
thanks
I want to get the index of First ever character appear in the string.
for example : I have a string " 225 get first character " In this I want the index of 'g' .how to get this ?
thanks
var str = " 225 get first character ";
var index = /[a-z]/i.exec(str).index;
alert(index); // 5
/i search for English letters, returning a zero based index of the match in string. Very nice!I have simplified @Jorgeblom answer:
var str = " 255 get first character";
var firstCharIndex = str.match('[a-zA-Z]').index;
console.log(firstCharIndex);
// 6
The match() function return a array with following values: