When I try to seach for a string with a point (i.e. '1.'), js also points to substrings with commas instead of points. It's better to look at the example:
'1,'.search('1.'); // 0
'xxx1,xxx'.search('1.'); // 3
// Normal behaviour
'1.'.search('1,'); // -1
Does anyone know why JavaScript behave itself so? Is there a way to search for exactly passed string?
Per the docs:
The
search()method executes a search for a match between a regular expression and thisStringobject.
. has a special meaning in Regular Expressions. You need to escape the . before matching it. Try the following:
console.log('xxx1,xxx'.search('1\\.'));
Use indexOf().
let str = "abc1,231.4";
console.log(str.indexOf("1."));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With