|
```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
};
}
``` |