Home > front end >  Js regular expression special characters, met a concealed bugs
Js regular expression special characters, met a concealed bugs

Time:10-09

 
Var reg=new RegExp (" [! @ # $% ^ & amp; () ` '_ :,,,,,] ")
Reg. The test (' Abc ')


Special characters in regular, literally knocked some,
But the test is a problem, as long as it contains uppercase characters can pass it, everything else is OK,
Even the same function of online example, also have this bug,

CodePudding user response:

Not to take the value
 

Var reg=new RegExp (" [! @ # $% ^ & amp; () ` '_ :,,,,,] ")
The console log (! Reg. The test (' Abc '))//true
The console log (! Reg. The test ('! Abc '))//false


What is your definition of a special character, it is your choice of these "! @ # $% ^ & amp; (a) ` '_ :; :; ,] ", or in addition to the upper and lower case letters and Numbers outside is special

CodePudding user response:

You are the regular string must be at least! @ # $% ^ & amp; (a) ` '_ :; :; , one of the special characters to return true,
Has nothing to do with contains contains uppercase characters
Var reg=new RegExp (" [! @ # $% ^ & amp; () ` '_ :,,,,,] ")
Alert (reg. Test (' Abc& '));//true

CodePudding user response:

That is just contain these special characters and not through,
I am the demo test (' Abc '), also returns false, so it was very strange

CodePudding user response:

reference michaelzhouh reply: 3/f
is just contain these special characters and not through,
Is the demo I test (' Abc '), also returns false, so strange
[! @ # $% ^ & amp; () ` '_ :,,,,,] you that this is the string does not contain these special characters are not through,
Contain special characters only through,
'Abc' does not contain special characters of a string of natural return false, not through, what is strange?
  • Related