I want to finn the indexOF first non-aplhabetic char from a certain postion on
Example
abc4sss5gggg10
I wan to to get the position of 5 but to specify where I start searching
I want to finn the indexOF first non-aplhabetic char from a certain postion on
Example
abc4sss5gggg10
I wan to to get the position of 5 but to specify where I start searching
Consider first this post here Return positions of a regex match() in Javascript?
Then know that to find the first numeric value you have to use
var n = 2; // my starting position
var match = /[0-9]/.exec("abc4sss5gggg10".substring(n));
if (match) {
alert("match found at " + match.index);
}
Use substring
to remove the first n characters
Your first thought might be regex, allthough those are heavy I'd probably go something like
getFirstNonAlpha(str) {
for (var i = 0; i<str.length;i++) {
if (!isNaN(str[i]) {
return i;
}
}
return false;
}
isNaN means isnotanumber so if it matches a number it'll return false and you can return the index.
Check which you need and which has a better speed if this is even an issue. Also this function won't help you with !@#$%^&*()_ etc.
DEMO