使用正则表达式删除子字符串

Delete Substring with regex

我想删除一个String中括号内的每个子串,例如

This is a (simple) text (or an example text). Thank you!

This is a text. Thank you!

我想用 replace 来完成,但找不到正常工作的 regExpression。

您可以替换以下正则表达式的结果:

\s?\([^)]+\)

空字符串。

替换

\s?\(.*?\)\s?

来自 space。演示:https://regex101.com/r/lU5wD2/2

这是您需要的正则表达式:

 / \(.+\)/

并且您需要像这样将其与替换一起使用:

 str = str.replace(/ \(.+\)/, "");