Javascript regular expression for punctuation (international)?

后端 未结 6 1279
难免孤独
难免孤独 2020-12-05 10:22

I need a regular expression to match against all punctuation marks, such as the standard [,!@#$%^&*()], but including international marks like the upside-down Spanish qu

6条回答
  •  一个人的身影
    2020-12-05 11:01

    From ES 2018, Unicode property escapes are supported. You can use \p{Punctuation} or just \p{P} (the same as the XRegExp answer) to match any punctuation character (by the Unicode definition), or \P{Punctuation} to match any non-punctuation character.

    If you want to match any "non-word" character, like a Unicode version of \W, you can try something like:

    [^\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]
    

    (as recommended in the proposal for the feature). You might want to remove \p{Connector_Punctuation}, since that includes underscores and similar.

    Don't forget to add the u flag to your regular expression to make it Unicode-aware and enable this feature.

提交回复
热议问题