Reputation: 8882
I have a javascript code which is supposed to test if some special characters are in a string like this:
var string = prompt("write something here","hello");
if(string.search("+") != -1){alert("special character")}
if(string.search("(") != -1){alert("special character")}
if(string.search(")") != -1){alert("special character")}
if(string.search("[") != -1){alert("special character")}
if(string.search("]") != -1){alert("special character")}
if(string.search("*") != -1){alert("special character")}
if(string.search("\\") != -1){alert("special character")}
For all the special characters above, it shows me an error message (which is different according to the special character, for + and * it says "unexpected quantificator" and for the others it says something about a regular expression). Why does it do that and what solutions are there? (I'm using HTA so don't suggest any HTML5-solutions)
Upvotes: 1
Views: 3410
Reputation: 700292
The search
method uses a regular expression, so all of the characters that you tried have to be escaped to be used that way.
If you want to look for a character, you should use the indexOf
method instead. Example:
if (string.indexOf("+") != -1) { alert("special character"); }
If you want the code in the question to work as intended, you can take advantage of the regular expression that search
uses, and check for all of the characters at once:
var string = prompt("write something here","hello");
if (string.search(/[+()[\]*\\]/) != -1) { alert("special character"); }
The regular expression concists of a character set ([]
) which contains the characters +()[]*\
. Note that the characters ]
and \
needs to be escaped as \]
and \\
. The other characters in the set doesn't need to be escaped, as they have no special meaning inside the set.
Upvotes: 1
Reputation:
String.prototype.search needs a regular expression object as parameter (read about it here). Therefore, in order to search for a special character you have to escape it first:
"anna+dan=?".search(/\+/)
on the other hand, it may be easier to just use String.prototype.indexOf (read about it here)
"anna+dan=?".indexOf("+")
Upvotes: 1
Reputation: 2486
You need to escape special characters. You can do something like this:
var string = prompt("write something here","hello");
if(string.search(regex_escape("+")) != -1){alert("special character")}
if(string.search(regex_escape("(")) != -1){alert("special character")}
if(string.search(regex_escape(")")) != -1){alert("special character")}
if(string.search(regex_escape("[")) != -1){alert("special character")}
if(string.search(regex_escape("]")) != -1){alert("special character")}
if(string.search(regex_escape("*")) != -1){alert("special character")}
if(string.search(regex_escape("\\")) != -1){alert("special character")}
function regex_escape(str) {
return str.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]', 'g'), '\\$&');
}
regex_escape() taken from Escape a '+' plus sign in a string to be used in a regex in coffeescript/javascript
Here is fiddle: http://jsfiddle.net/dahark0v/1/
Upvotes: 1