慕沐林林
2019-08-31 10:45:35
如果我有一個(gè)包含任何類型的非字母數(shù)字字符的字符串:"This., -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation"我如何在JavaScript中獲得它的無(wú)標(biāo)點(diǎn)版本:"This is an example of a string with punctuation"
3 回答

嚕嚕噠
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果要從字符串中刪除特定標(biāo)點(diǎn)符號(hào),最好明確刪除您想要的內(nèi)容
replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"")
執(zhí)行上述操作仍然不會(huì)返回指定字符串的字符串。如果你想刪除因刪除瘋狂標(biāo)點(diǎn)符號(hào)而留下的任何額外空格,那么你將要做類似的事情
replace(/\s{2,}/g," ");
我的完整例子:
var s = "This., -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation";
var punctuationless = s.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
var finalString = punctuationless.replace(/\s{2,}/g," ");
添加回答
舉報(bào)
0/150
提交
取消