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

后端 未结 6 1840
无人共我
无人共我 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: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.

?,./" id="ans_title" name="title">
提交回复
热议问题