我的要求是没有重复的特殊字符我目前有const reg=(/[^.*a-zA-Z0-9.,\s]*/g, '')我想允许sometext . something我不想让sometext,, something . some. some,
1 回答
扬帆大鱼
TA贡献1799条经验 获得超9个赞
如果您不想在完整的字符串中重复出现特殊字符。您可以使用match和Set
let nonRepeated = (str) => {
let match = str.match(/[.,]/g) || []
let setMatch = new Set(match)
return match.length != setMatch.size
}
console.log(nonRepeated('sometext . something'))
console.log(nonRepeated('sometext,, something . some. some,'))
如果你不想有连续的特殊字符,那么你可以使用这样做
let nonRepeated = (str) => !/([,.])(?=\1)/.test(str)
console.log(nonRepeated('sometext . something'))
console.log(nonRepeated('sometext,, something . some. some,'))
添加回答
举报
0/150
提交
取消