Regex for all PRINTABLE characters

前端 未结 6 1347
囚心锁ツ
囚心锁ツ 2020-12-11 01:09

Is there a special regex statement like \\w that denotes all printable characters? I\'d like to validate that a string only contains a character that can be printed--i.e. do

6条回答
  •  悲&欢浪女
    2020-12-11 01:21

    Very late to the party, but this regexp works: /[ -~]/.

    How? It matches all characters in the range from space (ASCII DEC 32) to tilde (ASCII DEC 126), which is the range of all printable characters.

    If you want to strip non-ASCII characters, you could use something like:

    $someString.replace(/[^ -~]/g, '');
    

    NOTE: this is not valid .net code, but an example of regexp usage for those who stumble upon this via search engines later.

提交回复
热议问题