简体   繁体   English

正则表达式在每个括号后添加一个新的换行符

[英]Regex to add a new line break after each bracket

我试过用正则表达式替换(/\\([(]\\)/g,'\\1\\n')但它没有帮助。有帮助吗?

Regardless what the language is, a round bracket matching pattern is either \\( or [(] . If you need to use a whole match value in the replacement, there are $& or $0 backreferences.不管是什么语言,圆括号匹配模式是\\([(] 。如果您需要在替换中使用整个匹配值,则有$&$0反向引用。

Thus, search for /[(]/g (you may add more chars into the character class, like [()\\][] ...) and replace with "$&\\n" (or "$0\\n" ).因此,搜索/[(]/g (您可以在字符类中添加更多字符,例如[()\\][] ...)并替换为"$&\\n" (或"$0\\n" ) .

See the regex demo .请参阅正则表达式演示

A JS demo:一个JS演示:

 var regex = /[(]/g; var str = "before(after and before 1(after1"; var subst = "$&\\n"; var result = str.replace(regex, subst); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM