BIG陽
2022-06-16 09:57:41
我的要求是沒有重復的特殊字符我目前有const reg=(/[^.*a-zA-Z0-9.,\s]*/g, '')我想允許sometext . something我不想讓sometext,, something . some. some,
1 回答

揚帆大魚
TA貢獻1799條經(jīng)驗 獲得超9個贊
如果您不想在完整的字符串中重復出現(xiàn)特殊字符。您可以使用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,'))
如果你不想有連續(xù)的特殊字符,那么你可以使用這樣做
let nonRepeated = (str) => !/([,.])(?=\1)/.test(str)
console.log(nonRepeated('sometext . something'))
console.log(nonRepeated('sometext,, something . some. some,'))
添加回答
舉報
0/150
提交
取消