我想使用JavaScript从字符串中删除除空格之外的所有特殊字符。

例如, 美国广播公司的测试#年代 应输出为 abc测试。


当前回答

const input = ' #if_1 $(PR_CONTRACT_END_DATE) == '23-09-2019' # Test27919<alerts@imimobile.com> #elseif_1 $(PR_CONTRACT_START_DATE) == '20-09-2019' # Sender539<rama.sns@gmail.com> #elseif_1 $(PR_ACCOUNT_ID) == '1234' # AdestraSID < hello@imimobile.co > # else_1 # Test27919 < alerts@imimobile.com > # endif_1 # '; const replaceString = input.split (' $ (') . join(“- >”).split (') ') . join(“< -”); console.log (replaceString.match (/(?<=->).*?(?=<-)/ g));

其他回答

你可以指定你想要删除的字符:

string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');

或者,要更改除数字和字母以外的所有字符,请尝试:

string = string.replace(/[^a-zA-Z0-9]/g, '');

正则表达式

let string  = "!#This tool removes $special *characters* /other/ than! digits, characters and spaces!!!$";

var NewString= string.replace(/[^\w\s]/gi, '');
console.log(NewString); 

结果//删除除数字、字符和空格以外的特殊字符

实例:https://helpseotools.com/text-tools/remove-special-characters

我不知道JavaScript,但它不是可能使用正则表达式?

像[^\w\d\s]这样的东西可以匹配除数字、字符和空白之外的任何东西。在JavaScript中找到语法只是一个问题。

你应该使用字符串替换函数,一个单一的正则表达式。 假设你所说的特殊字符是指任何不是字母的字符,下面是一个解决方案:

Const STR = "abc's test#s"; console.log (str。replace(/[^a-zA-Z]/g, "");

试试这个:

const strippedString = htmlString.replace(/(<([^>]+)>)/gi, "");
console.log(strippedString);