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

时间:2017-09-28 11:56:00

标签: javascript regex

我有正则表达式的问题

我有这样的台词:

Line1
Line2
/
Line3
Line4

我希望在每行之后添加一个新行,除了以/开头的行 目标:

Line1
#
Line2
#
/
Line3
#
Line4
#

我没有通过单一的正则表达式调用来实现这一目标。它总是只替换第一行或1& 3

1 个答案:

答案 0 :(得分:0)

您可以使用'/(^[^\/].*)/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);




相关问题