检查字符串中的特殊字符。

时间:2021-05-18 01:01:49

I want to check if a string contains special characters like !@#$%^&*.,<>/\'";:? and return true if the string contains atleast one of those chars.

我想要检查一个字符串是否包含特殊字符喜欢! @ # $ % ^ & *。< > / \ '”;:?如果字符串包含至少一个字符,则返回true。

I tried with the following regex,script:

我尝试使用以下regex脚本:

var format = /^[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/;

if( string.match(format) ){
  return true;
}else{
  return false;
}

If the string contains only the special characters then it returns true , but if the string contains something else like alphanumeric chars ( !example1 , .example2 ) it returns false.

如果字符串只包含特殊字符,那么它将返回true,但是如果字符串包含了其他类似于字母数字字符的字符(!example1, .example2),它将返回false。

6 个解决方案

#1


9  

I suggest using RegExp .test() function to check for a pattern match, and the only thing you need to change is remove the start/end of line anchors (and the * quantifier is also redundant) in the regex:

我建议使用RegExp .test()函数检查模式匹配,惟一需要更改的是删除regex中的线锚的开始/结束(*量词也是冗余的):

var format = /[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;
//            ^                                    ^   
document.write(format.test("My@string-with(some%text)") + "<br/>");
document.write(format.test("My string with spaces") + "<br/>");
document.write(format.test("MyStringContainingNoSpecialChars"));

The anchors (like ^ start of string/line, $ end od string/line and \b word boundaries) can restrict matches at specific places in a string. When using ^ the regex engine checks if the next subpattern appears right at the start of the string (or line if /m modifier is declared in the regex). Same case with $: the preceding subpattern should match right at the end of the string.

锚(比如^字符串/线,开始结束美元od字符串/线,\ b单词边界)可以限制在特定的地方匹配字符串。当使用^正则引擎检查如果第二子模式出现在字符串的开始(或者如果/ m修饰符是正则表达式中声明)。$也是一样:前面的子模式应该在字符串的末尾匹配。

In your case, you want to check the existence of the special character from the set anywhere in the string. Even if it is only one, you want to return false. Thus, you should remove the anchors, and the quantifier *. The * quantifier would match even an empty string, thus we must remove it in order to actually check for the presence of at least 1 special character (actually, without any quantifiers we check for exactly one occurrence, same as if we were using {1} limiting quantifier).

在您的示例中,您希望检查字符串中任何地方的集合中是否存在特殊字符。即使只有一个,你也要返回false。因此,您应该删除锚点和量词*。*量词甚至会匹配一个空字符串,因此我们必须删除它,以便实际检查至少有一个特殊字符存在(实际上,没有任何量词,我们只检查一次出现,就像使用{1}限制量词一样)。

#2


5  

Your regexp use ^ and $ so it tries to match the entire string. And if you want only a boolean as the result, use test instead of match.

正则表达式使用^和$所以试图匹配整个字符串。如果你只想要一个布尔值作为结果,使用测试而不是匹配。

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;

if(format.test(string)){
  return true;
} else {
  return false;
}

#3


3  

Wouldn't it be easier to negative-match alphanumerics instead?

否定-匹配字母数字不是更容易吗?

return string.match(/^[^a-zA-Z0-9]+$/) ? true : false;

#4


1  

var specialChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=";
var checkForSpecialChar = function(string){
 for(i = 0; i < specialChars.length;i++){
   if(string.indexOf(specialChars[i]) > -1){
       return true
    }
 }
 return false;
}

var str = "YourText";
if(checkForSpecialChar(str)){
  alert("Not Valid");
} else {
    alert("Valid");
}

#5


1  

Remove the characters ^ (start of string) and $ (end of string) from the regular expression.

开始删除字符^(字符串)和$(结束字符串)的正则表达式。

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;

#6


-1  

You can try this:

你可以试试这个:

regex = [\W_]

regex =[\ W_]

It will definitely help you.

这肯定对你有帮助。

#1


9  

I suggest using RegExp .test() function to check for a pattern match, and the only thing you need to change is remove the start/end of line anchors (and the * quantifier is also redundant) in the regex:

我建议使用RegExp .test()函数检查模式匹配,惟一需要更改的是删除regex中的线锚的开始/结束(*量词也是冗余的):

var format = /[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;
//            ^                                    ^   
document.write(format.test("My@string-with(some%text)") + "<br/>");
document.write(format.test("My string with spaces") + "<br/>");
document.write(format.test("MyStringContainingNoSpecialChars"));

The anchors (like ^ start of string/line, $ end od string/line and \b word boundaries) can restrict matches at specific places in a string. When using ^ the regex engine checks if the next subpattern appears right at the start of the string (or line if /m modifier is declared in the regex). Same case with $: the preceding subpattern should match right at the end of the string.

锚(比如^字符串/线,开始结束美元od字符串/线,\ b单词边界)可以限制在特定的地方匹配字符串。当使用^正则引擎检查如果第二子模式出现在字符串的开始(或者如果/ m修饰符是正则表达式中声明)。$也是一样:前面的子模式应该在字符串的末尾匹配。

In your case, you want to check the existence of the special character from the set anywhere in the string. Even if it is only one, you want to return false. Thus, you should remove the anchors, and the quantifier *. The * quantifier would match even an empty string, thus we must remove it in order to actually check for the presence of at least 1 special character (actually, without any quantifiers we check for exactly one occurrence, same as if we were using {1} limiting quantifier).

在您的示例中,您希望检查字符串中任何地方的集合中是否存在特殊字符。即使只有一个,你也要返回false。因此,您应该删除锚点和量词*。*量词甚至会匹配一个空字符串,因此我们必须删除它,以便实际检查至少有一个特殊字符存在(实际上,没有任何量词,我们只检查一次出现,就像使用{1}限制量词一样)。

#2


5  

Your regexp use ^ and $ so it tries to match the entire string. And if you want only a boolean as the result, use test instead of match.

正则表达式使用^和$所以试图匹配整个字符串。如果你只想要一个布尔值作为结果,使用测试而不是匹配。

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;

if(format.test(string)){
  return true;
} else {
  return false;
}

#3


3  

Wouldn't it be easier to negative-match alphanumerics instead?

否定-匹配字母数字不是更容易吗?

return string.match(/^[^a-zA-Z0-9]+$/) ? true : false;

#4


1  

var specialChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=";
var checkForSpecialChar = function(string){
 for(i = 0; i < specialChars.length;i++){
   if(string.indexOf(specialChars[i]) > -1){
       return true
    }
 }
 return false;
}

var str = "YourText";
if(checkForSpecialChar(str)){
  alert("Not Valid");
} else {
    alert("Valid");
}

#5


1  

Remove the characters ^ (start of string) and $ (end of string) from the regular expression.

开始删除字符^(字符串)和$(结束字符串)的正则表达式。

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;

#6


-1  

You can try this:

你可以试试这个:

regex = [\W_]

regex =[\ W_]

It will definitely help you.

这肯定对你有帮助。