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
1 Answer
Reset to default 4There 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
版权声明:本文标题:javascript - search for backslash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744887986a2630600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论