简体   繁体   中英

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.

Thus, search for /[(]/g (you may add more chars into the character class, like [()\\][] ...) and replace with "$&\\n" (or "$0\\n" ).

See the regex demo .

A JS demo:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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