11 条回复  ·  1362 次点击
seakee 小成 2024-12-9 16:27:48
```js function checkSpecialCharacters(str) { // 允许的特殊字符 const allowedSpecials = ['.', '-', '&', ':']; // 使用正则表达式匹配所有非字母数字的字符 // \p{L} 匹配任何语言的字母(包括中文、英文、阿拉伯语等) // \p{N} 匹配任何数字 const specialChars = str.match(/[^\p{L}\p{N}]/gu) || []; // 存储非法的特殊字符 const illegalChars = specialChars.filter(char => !allowedSpecials.includes(char)); return { hasIllegalChars: illegalChars.length > 0, illegalChars: [...new Set(illegalChars)], // 去重 isValid: illegalChars.length === 0 }; } ```
festoney8 初学 2024-12-9 16:30:38
三楼用 unicode general category 的方法应该可行,但 Number 大类下有很多奇怪字符,不知道你对这些的定义算不算符号,具体可以对照文档细调分类 https://unicode.org/reports/tr18/#General_Category_Property
12
返回顶部