我想从字符串中删除括号和正斜杠或反斜杠 ()/\
。
例如...Nort()h/(America)
应为北美
还有一些其他情况。
- 北////()美洲 => 北美洲
- (北美)=> 北美
- ((((()()()()(()()否(())))))))rth///////(()))()((()///\\\\\\美国///////' => 北美 等等..
最佳答案
使用正则表达式替换
"Nort()h / (America)".replace( /[()\\\/]/g, "" ) //outputs North America
说明
[()\\\/]
是()\/
的字符集,其中反斜杠和正斜杠均被转义。
此外,如果您希望将多个连续空格替换为单个空格,则添加
var input = "Nort()h / (America)";
var output = input.replace( /[()\\\/]/g, "" ).replace( /\s+/, " " ); //North America
编辑
虽然你的逻辑不清楚,但你可以尝试以下方法
var output = input.replace( /\/{2,}/g, " " ) //replace consecutive / with space
.replace( /\\/g, " " ) //replace consecutive \ with space
.replace( /[()\\\/]/g, "" )
.replace( /\s+/, " " );
关于javascript - 正则表达式删除括号和/(正斜杠)形成字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48860190/