Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./

后端 未结 6 1839
无人共我
无人共我 2020-11-28 19:20

I\'m trying to create a Regex test in JavaScript that will test a string to contain any of these characters:

!$%^&am         


        
相关标签:
6条回答
  • 2020-11-28 19:36

    A simple way to achieve this is the negative set [^\w\s]. This essentially catches:

    • Anything that is not an alphanumeric character (letters and numbers)
    • Anything that is not a space, tab, or line break (collectively referred to as whitespace)

    For some reason [\W\S] does not work the same way, it doesn't do any filtering. A comment by Zael on one of the answers provides something of an explanation.

    0 讨论(0)
  • 2020-11-28 19:37
    // The string must contain at least one special character, escaping reserved RegEx characters to avoid conflict
      const hasSpecial = password => {
        const specialReg = new RegExp(
          '^(?=.*[!@#$%^&*"\\[\\]\\{\\}<>/\\(\\)=\\\\\\-_´+`~\\:;,\\.€\\|])',
        );
        return specialReg.test(password);
      };
    
    0 讨论(0)
  • 2020-11-28 19:44

    The most simple and shortest way to accomplish this:

    /[^\p{L}\d\s@#]/u
    

    Explanation

    [^...] Match a single character not present in the list below

    • \p{L} => matches any kind of letter from any language

    • \d => matches a digit zero through nine

    • \s => matches any kind of invisible character

    • @# => @ and # characters

    Don't forget to pass the u (unicode) flag.

    0 讨论(0)
  • 2020-11-28 19:44

    Replace all latters from any language in 'A', and if you wish for example all digits to 0:

    return str.replace(/[^\s!-@[-`{-~]/g, "A").replace(/\d/g, "0");
    
    0 讨论(0)
  • 2020-11-28 19:48

    Answer

    /[\W\S_]/
    

    Explanation

    This creates a character class removing the word characters, space characters, and adding back the underscore character (as underscore is a "word" character). All that is left is the special characters. Capital letters represent the negation of their lowercase counterparts.

    \W will select all non "word" characters equivalent to [^a-zA-Z0-9_]
    \S will select all non "whitespace" characters equivalent to [ \t\n\r\f\v]
    _ will select "_" because we negate it when using the \W and need to add it back in

    0 讨论(0)
  • 2020-11-28 19:52

    The regular expression for this is really simple. Just use a character class. The hyphen is a special character in character classes, so it needs to be first:

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

    You also need to escape the other regular expression metacharacters.

    Edit: The hyphen is special because it can be used to represent a range of characters. This same character class can be simplified with ranges to this:

    /[$-/:-?{-~!"^_`\[\]]/
    

    There are three ranges. '$' to '/', ':' to '?', and '{' to '~'. the last string of characters can't be represented more simply with a range: !"^_`[].

    Use an ACSII table to find ranges for character classes.

    0 讨论(0)
?,./" id="ans_title" name="title">
提交回复
热议问题