正则表达式在每次匹配之前插入字符串

Cha*_*sEF 6 javascript regex

我搜索过并找到了一些解决方案,但它们适用于不同的语言,我无法翻译它们。由于我对此了解甚少,希望向专家求助。这是我的技能所能达到的程度:

到目前为止我的代码:

var str = "Tell us about this interaction. Make sure you mention: (a) what the interaction was about (hangout, chit chat, discuss assignments, etc.) (b) what other things you and/or your friend was doing on the phone during this interaction (c) whether both of you were involved in this other activity and (d) any other information about this interaction you would like to share with us. Avoid using your friends full real name";
var pat = /\([a-zA-Z]\)/g;
var out = str.replace(pat, "<br>");
alert(out);
Run Code Online (Sandbox Code Playgroud)

此代码仅将匹配项替换为“<br>”,忘记空格,论坛将翻译新行。

我想要得到的输出是这样的

“告诉我们这次互动的情况。请务必提及:<br>(a) 互动的内容(环聊、闲聊、讨论作业等)<br>(b) 您和/或您的朋友还有哪些其他事情在此互动期间您正在打电话<br>(c) 你们俩是否参与了其他活动以及<br>(d) 您想与我们分享的有关此次互动的任何其他信息。避免充分利用您的朋友真正的名字”

感谢您的任何帮助,

查尔斯

nic*_*ael 12

神奇的$&是你正在寻找的东西:)

var str = "Tell us about this interaction. Make sure you mention: (a) what the interaction was about (hangout, chit chat, discuss assignments, etc.) (b) what other things you and/or your friend was doing on the phone during this interaction (c) whether both of you were involved in this other activity and (d) any other information about this interaction you would like to share with us. Avoid using your friends full real name";
var pat = /\([a-zA-Z]\)/g;
var out = str.replace(pat, "<br>$&");
document.write(out);
Run Code Online (Sandbox Code Playgroud)