admin管理员组

文章数量:1405170

I've JavaScript which searches for eg. a letter in a string and outputs "ok" if the letter is not there and "not ok" if the letter is there:

 var term = "term";
 var slash = "a";
 var search =term.search(slash);

 if(search==-1)
 "ok";
 else
 "not ok";

The problem is that I want this to work with a backslash too. The strange thing is, searching for 2 backslashes in a row works, so "term" outputs "ok" and "term\\" outputs "not ok":

 var term = "term";
 var slash = "\\\\";
 var search =term.search(slash);

 if(search==-1)
 "ok";
 else
 "not ok";

But searching for 1 backslash doesn't work, so this code gives an error:

 var term = "term";
 var slash = "\\";
 var search =term.search(slash);

 if(search==-1)
 "ok";
 else
 "not ok";

Hope someone sees the error. Thanks!

I've JavaScript which searches for eg. a letter in a string and outputs "ok" if the letter is not there and "not ok" if the letter is there:

 var term = "term";
 var slash = "a";
 var search =term.search(slash);

 if(search==-1)
 "ok";
 else
 "not ok";

The problem is that I want this to work with a backslash too. The strange thing is, searching for 2 backslashes in a row works, so "term" outputs "ok" and "term\\" outputs "not ok":

 var term = "term";
 var slash = "\\\\";
 var search =term.search(slash);

 if(search==-1)
 "ok";
 else
 "not ok";

But searching for 1 backslash doesn't work, so this code gives an error:

 var term = "term";
 var slash = "\\";
 var search =term.search(slash);

 if(search==-1)
 "ok";
 else
 "not ok";

Hope someone sees the error. Thanks!

Share Improve this question edited Jan 21, 2012 at 14:37 skaffman 404k96 gold badges824 silver badges775 bronze badges asked Jan 21, 2012 at 14:35 StevenSteven 1373 silver badges13 bronze badges 3
  • 3 Have you noticed that all you term Varaiables doesn't contain backslashes?? – Adel Boutros Commented Jan 21, 2012 at 14:37
  • 2 So how are you searching for something that does not exist? – Adel Boutros Commented Jan 21, 2012 at 14:37
  • It's an example, in my examples, the output would be "ok"...since the var search outputs -1 (since there is no backslash in the var term). – Steven Commented Jan 21, 2012 at 14:40
Add a ment  | 

1 Answer 1

Reset to default 4

There are two layers of interpretation involved with making a regular expression in JavaScript. The first is that of the string syntax, and you've correctly doubled your backslashes to account for that. However, the string will itself be interpreted by the regular expression syntax analysis code, and that will have a problem with a single lone backslash. In other words, a regular expression consisting of a single backslash is a syntax error; it's simply not allowed. If you want to search for a single backslash, you need a regular expression with two backslashes in it.

Making a regular expression with the native literal regular expression syntax makes this more obvious:

var r1 = /\\/; /* this is OK */
var r2 = /\/;  /* this is not OK */

本文标签: javascriptsearch for backslashStack Overflow