JavaScript search() fails to find “()”

后端 未结 2 1385
余生分开走
余生分开走 2021-01-18 10:23

This might seem trivial, but I\'m new to JS. I have this piece of code:

alert(elementAction);    
var argumentsBegin = elementAction.search(\"(\");
var argum         


        
2条回答
  •  天命终不由人
    2021-01-18 10:52

    Yes: the search() method of strings expects a regular expression as the parameter and is treating the string you're passing as a regular expression pattern, in which parentheses have special meaning. Use indexOf() instead:

    alert( elementAction.indexOf("(") ); 
    

提交回复
热议问题