-4

我希望能够使用正则表达式在字符串中查找时间范围,然后将它们包装在 <span> 中。时间范围的格式如下:

Hello, I would like to meet between 9:00-12:00 but also 1:00-6:00 would work for me. Thank you.

我希望最终输出为:

Hello, I would like to meet between <span>9:00-12:00</span> but also <span>1:00-6:00</span> would work for me. Thank you.

4

2 回答 2

2
mystring.replace(/(\d{1,2}\:\d{2}\-\d{1,2}\:\d{2})/g,"<span>$1</span>")

快!不过超级危险的东西。

于 2012-07-11T12:48:55.050 回答
2

这样的事情应该这样做:

var str = "your string here...";

str = str.replace(/(\d\d?:\d\d-\d\d?:\d\d)/g,"<span>$1</span>");

\d虽然匹配任何数字,所以上面接受“时间”之类的99:99- 您可能希望将其缩小到适当的数字范围。也许是这样的:

/((1[0-2]|\d):[0-5]\d-(1[0-2]|\d):[0-5]\d)/g
于 2012-07-11T12:49:09.707 回答