我想使用JavaScript从字符串中删除除空格之外的所有特殊字符。
例如, 美国广播公司的测试#年代 应输出为 abc测试。
我想使用JavaScript从字符串中删除除空格之外的所有特殊字符。
例如, 美国广播公司的测试#年代 应输出为 abc测试。
当前回答
Const STR = "abc's@thy#^g&test#s"; console.log (str。replace(/[^a-zA-Z]/g, "");
其他回答
你想从字符串中删除谁的特殊字符,准备一个列表,然后使用javascript替换函数删除所有特殊字符。
var str = 'abc'de#;:sfjkewr47239847duifyh';
alert(str.replace("'","").replace("#","").replace(";","").replace(":",""));
或者你可以为整个字符串运行循环,并将单个单个字符与ASCII码进行比较,并重新生成一个新字符串。
试着用这个
var result= stringToReplace.replace(/[^\w\s]/g, '')
[^]表示否定,\w表示[a-zA-Z0-9_]字字符,\s表示空格, /[]/g表示全局
你可以指定你想要删除的字符:
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
或者,要更改除数字和字母以外的所有字符,请尝试:
string = string.replace(/[^a-zA-Z0-9]/g, '');
你应该使用字符串替换函数,一个单一的正则表达式。 假设你所说的特殊字符是指任何不是字母的字符,下面是一个解决方案:
Const STR = "abc's test#s"; console.log (str。replace(/[^a-zA-Z]/g, "");
试试这个:
const strippedString = htmlString.replace(/(<([^>]+)>)/gi, "");
console.log(strippedString);