-1

我在 Javascript 中有这个字符串:

text = "2222 22:22: John: New York /n last year and: the next year /n 3333 33:33: Jack: Los Angeles!!!"

我希望它是以下内容:

newText = "(xSep)2222 22:22:(zSep) John:(zSep) New York /n last year and: the next year /n (xSep)3333 33:33:(zSep) Jack:(zSep) Los Angeles!!!"

我已经尝试了很多东西,只有这样我的其余代码才能工作。所以“(xSep)”应该出现 每四个数字之前,“(zSep)”应该只出现在前 两个“:”之后

请帮我解决一下这个。提前谢谢你。

4

2 回答 2

1

根据您给定的输出和要求,您可以使用以下内容。

var res = str.replace(/(\d{4}(?:[^:]*:){2})([^:]*:)/g, '(xSep)$1(zSep)$2(zSep)');

工作演示

于 2014-12-01T16:29:45.307 回答
0

只需使用带有正则表达式的 string.replace 方法作为针。

于 2014-12-01T16:15:05.840 回答