简体   繁体   English

JavaScript 正则表达式:匹配行并添加新行

[英]JavaScript Regex: Match lines and add new line

I have a problem with a regex expression我的正则表达式有问题

I have lines like this:我有这样的行:

Line1
Line2
/
Line3
Line4

I'd like to add a new line after each line EXCEPT the lines starting with / Goal:我想在每行之后添加一个新行,除了以 / Goal 开头的行:

Line1
#
Line2
#
/
Line3
#
Line4
#

I don't get how to realize this with a single regex call.我不知道如何通过一个正则表达式调用来实现这一点。 It always replaces only the first line or 1&3它总是只替换第一行或 1&3

You can use '/(^[^\\/].*)/gm' as a regex您可以使用'/(^[^\\/].*)/gm'作为正则表达式

 const regex = /(^[^\\/].*)/gm; const str = `Line1 Line2 / Line3 Line4`; const subst = `$1\\n`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log(result);

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

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