我想从字符串中删除特殊字符,并用 _字符替换它们。
_
例如:
string = "img_realtime_tr~ading3$"
生成的字符串应该类似于“ img _ realtime _ tr _ ading3 _”;
我需要替换这些字符: & / \ # , + ( ) $ ~ % .. ' " : * ? < > { }
& / \ # , + ( ) $ ~ % .. ' " : * ? < > { }
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_');
Alternatively, to change all characters except numbers and letters, try:
string = string.replace(/[^a-zA-Z0-9]/g,'_');
string = string.replace(/[\W_]/g, "_");