【发布时间】:2021-03-03 12:17:03
【问题描述】:
我正在努力在字符串中“注释:”之后出现的任何日期之前插入换行符。
我的正则表达式似乎在“注释:”之后的第一个日期之前捕获所有文本:
非常感谢您对 JavaScript 的任何帮助。
const mystring = 'wed and thurs and notes: are just so interesting 02-03-2019 on a new line please 04-05-2020 on another line please'
mystring.replaceAll(/(?<=notes:).*?(\d{1,2}-\d{1,2}-\d{4})/g, function(capture){
return '<br>' + capture;
}
);
我想要的输出:
wed and thrus and notes: are just so interesting <br> 02-03-2019 on a new line please <br> 04-05-2020 on another line please
【问题讨论】:
-
mystring.replace(/(?<=notes:.*?)\d{1,2}-\d{1,2}-\d{4}/g, '<br>$&')? -
您是否介意解释一下,我如何捕获带有“-”或“/”的日期,即 02-03-2002 或 02/03/2002
标签: javascript regex replace replaceall