1

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)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 4
    .search seems to be using regular expressions, therefore you probably wanted to use .indexOf instead. .search, in fact, because of the fact that accepts regular expressions, will always prompt you if you're using some characters that are related to regular expressions. Using .indexOf instead of .search everything will work as expected. http://jsfiddle.net/Lhkhvxwg/ – briosheje Apr 26 '15 at 12:13
  • 3
    Read the [**documentation**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search), `string.search` accepts a **regular expression** ! – adeneo Apr 26 '15 at 12:13
  • 2
    uh, `string.search()` is not exactly the "javascript replace method"?! – Bergi Apr 26 '15 at 12:17
  • @Bergi: Not sure, it seems that instead of returning the matches it returns the position of the match inside the string. – briosheje Apr 26 '15 at 12:22
  • @briosheje: If you want to return matches, use `match` or `exec` (which are not `replace` either) – Bergi Apr 26 '15 at 12:26
  • @Bergi : Indeed, true. No idea. I just think that using .search, in such a case, has no sense, because .indexOf does that job beautifully :P – briosheje Apr 26 '15 at 12:27
  • @briosheje: Both `search` and `indexOf` return positions, but one is for regexes while the other is for strings. – Bergi Apr 26 '15 at 12:31
  • @Bergi You're right, it's a mistake that I corrected. I was editing another part of that program which uses the `search()` method while I was writing this, that's why I made the mistake. – Donald Duck Apr 26 '15 at 12:38

3 Answers3

1

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("+")
1

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/

Community
  • 1
  • 1
Said Kholov
  • 2,436
  • 1
  • 15
  • 22
1

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005