简体   繁体   English

如何检查JavaScript中是否存在区分大小写和特殊字符的变量中的特定字符串?

[英]How to check if particular string is present in variable with case sensitive and special characters in javascript?

I am having string this way, 我是这样子的

"The save operation will overwrite [data] in DB;." “保存操作将覆盖DB;中的[数据]。”

I am verifying this way to find is the above string is present in a particular variable. 我正在验证以这种方式查找是否上述字符串存在于特定变量中。

var myVar  = "The save operation will overwrite [data] in DB;.";
                    var pattern = /The save operation will overwrite [data] in DB;./;
                    var exists = pattern.test(myVar);
                    alert(exists);

why even though pattern and string myVar is matached.. exists is returning false only IN THIS CASE? 为什么即使模式和字符串myVar被匹配..存在也仅在这种情况下返回false?

I want to exists = true if only if pattern string =='The save operation will overwrite [data] in DB;.' 仅当模式字符串==“保存操作将覆盖数据库中的[数据]时,我才想存在= true”。

for rest of all the cases exists should be = false. 对于其余所有情况,应为=假。

ex: The save operation will update/overwrite [data] in DB;. 例如:保存操作将更新/覆盖DB;中的[数据]。 should also return false. 还应该返回false。

Exist should be true if and only if exactly this pattern is matched. 当且仅当此模式完全匹配时,existing应该为true。 'The save operation will overwrite [data] in DB;.' “保存操作将覆盖DB;中的[数据]。”

Where exactly I am doing mistake? 我到底在哪里犯错?

Thanks in Advance. 提前致谢。

You are using pattern matching, and therfor you should escape special characters ( http://www.regular-expressions.info/refcharacters.html ). 您正在使用模式匹配,因此应转义特殊字符( http://www.regular-expressions.info/refcharacters.html )。

The correct syntax should be 正确的语法应该是

var myVar  = "The save operation will overwrite [data] in DB;.";
                var pattern = /^The save operation will overwrite \[data\] in DB;\.$/;
                var exists = pattern.test(myVar);
                alert(exists);

Note the escape characters before '[', ']' and '.'. 注意“ [”,“]”和“。”之前的转义字符。 ALso the caret '^' tells the line should start there, and the dollar sign '$' the line should end there. 同样,脱字号'^'告诉该行应从此处开始,而美元符号'$'该行应从此处结束。

If you want to do case invariant, you can add 'i'. 如果要进行大小写不变,则可以添加“ i”。 For multiple lines, add 'g' 对于多行,请添加“ g”

var myVar  = "The save operation will overwrite [data] in DB;.";
                var pattern = /^The save operation will overwrite \[data\] in DB;\.$/gi;
                var exists = pattern.test(myVar);
                alert(exists);

You can use this site to test regular expressions. 您可以使用此站点测试正则表达式。

You don't need regular expressions at all here, see below three "match" versions, use the one that fits your requirements: 您在这里根本不需要正则表达式,请参见下面的三个“匹配”版本,使用符合您要求的版本:

 var myVar = 'The save operation will overwrite [data] in DB;.'; var pattern = 'The save operation will overwrite [data] in DB;.'; var exists = myVar.indexOf(pattern) > -1; var startsWith = myVar.indexOf(pattern) == 0; var strictEqual = myVar === pattern; alert('Exists: ' + exists + '\\n' + 'Starts with: ' + startsWith + '\\n' + 'Strict equal: ' + strictEqual); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM