是否有可能改变
Hello, this is Mike (example)
Run Code Online (Sandbox Code Playgroud)
至
Hello, this is Mike
Run Code Online (Sandbox Code Playgroud)
在正则表达式中使用JavaScript?
the*_*ejh 182
"Hello, this is Mike (example)".replace(/ *\([^)]*\) */g, "");
Run Code Online (Sandbox Code Playgroud)
结果:
"Hello, this is Mike"
Run Code Online (Sandbox Code Playgroud)
Tat*_*nen 24
var str = "Hello, this is Mike (example)";
alert(str.replace(/\s*\(.*?\)\s*/g, ''));
Run Code Online (Sandbox Code Playgroud)
这也将取代括号前后的多余空格.
Mam*_*mun 11
尝试 / \([\s\S]*?\)/g
在哪里
(
空格) 字面上匹配字符(空格)
\(
(
字面上匹配字符
[\s\S]
匹配任何字符(\s
匹配任何空白字符并\S
匹配任何非空白字符)
*?
零次和无限次之间的匹配
\)
)
字面上匹配字符
g
全球匹配
代码示例:
var str = "Hello, this is Mike (example)";
str = str.replace(/ \([\s\S]*?\)/g, '');
console.log(str);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {top: 0}
Run Code Online (Sandbox Code Playgroud)
如果您还需要删除嵌套括号内的文本,那么:
var prevStr;
do {
prevStr = str;
str = str.replace(/\([^\)\(]*\)/, "");
} while (prevStr != str);
Run Code Online (Sandbox Code Playgroud)