Why is it a bug in Search?

Code 'sometext(sometext'.search('(') throws an error SyntaxError: Invalid regular expression: /(/: Unterminated group at the String.search () at :1:21.

How can I bypass or fix?
April 7th 20 at 10:40
1 answer
April 7th 20 at 10:42
Solution
The search method uses regular expression search, and if the argument is not, it will be converted into the regular season. Parentheses in regular expressions are special characters, so if you want the brace was just a parenthesis, it should be shielded. Put in front of her two backslashes: .search('\\('). Or one, if you replace the string on a regular expression literal: .search(/\(/).
And if the search string entered by the user via input in the string contains parenthesis? how then to be - Gaston_Veum commented on April 7th 20 at 10:45
Obviously, replace the bracket on the brace with slashes: str.replace(/\(/g, '\\(').
Or search is to use indexOf. - Willa.Carroll commented on April 7th 20 at 10:48
@Willa.Carroll, thank you - Gaston_Veum commented on April 7th 20 at 10:51

Find more questions by tags JavaScript