我已经编写了以下允许所有国际字符(拉丁语、亚洲语……)的正则表达式
\n 'D\xc3\xbcsseldorf, K\xc3\xb6ln, \xd0\x9c\xd0\xbe\xd1\x81\xd0\xba\xd0\xb2\xd0\xb0, \xe5\x8c\x97\xe4\xba\xac\xe5\xb8\x82, \xd8\xa5\xd8\xb3\xd8\xb1\xd8\xa7\xd8\xa6\xd9\x8a\xd9\x84 !@#$'.match(/[\\p{L}-]+/ug)\nRun Code Online (Sandbox Code Playgroud)\n但我想让它不允许所有特殊字符,例如!?})%....
仅包含字母、数字、破折号、点、逗号和空格的匹配字符串:
\nconsole.log(\n /^[\\p{L},.0-9\\s-]+$/u.test(\'D\xc3\xbcsseldorf, K\xc3\xb6ln, \xd0\x9c\xd0\xbe\xd1\x81\xd0\xba\xd0\xb2\xd0\xb0, \xe5\x8c\x97\xe4\xba\xac\xe5\xb8\x82, \xd8\xa5\xd8\xb3\xd8\xb1\xd8\xa7\xd8\xa6\xd9\x8a\xd9\x84 !@#$\')\n)\nconsole.log(\n /^[\\p{L},.0-9\\s-]+$/u.test(\'D\xc3\xbcsseldorf, K\xc3\xb6ln, \xd0\x9c\xd0\xbe\xd1\x81\xd0\xba\xd0\xb2\xd0\xb0, \xe5\x8c\x97\xe4\xba\xac\xe5\xb8\x82, \xd8\xa5\xd8\xb3\xd8\xb1\xd8\xa7\xd8\xa6\xd9\x8a\xd9\x84\')\n)Run Code Online (Sandbox Code Playgroud)\r\n结果:false和true。
解释
\n-------------------------------------------------------------------------------\n ^ the beginning of the string\n--------------------------------------------------------------------------------\n [\\p{L},.0-9\\s-]+ any character of: letter, \',\', \'.\',\n \'0\' to \'9\', whitespace (\\n, \\r, \\t, \\f,\n and " "), \'-\' (1 or more times (matching\n the most amount possible))\n--------------------------------------------------------------------------------\n $ before an optional \\n, and the end of the\n string\nRun Code Online (Sandbox Code Playgroud)\n