function fun() {
var temp = document.getElementById('DIV2').innerHTML;
var str = temp;
var res = str.replace(/(\s(\S*m)\s)/gi, ' <span class="red">$1</span> '); // 0.5 -- 1m -- etc
var res2 = res.replace(/\s(\S*metre)\s/gi, '<span class="red">$1</span> '); // 1meter 2meter
var res3 = res.replace(/\s(\S\s*metre)\s/gi, '<span class="red">$1</span> '); // 1 meter 2 meter
document.body.innerHTML = res3;
}
<body>
<div id="text">
The Basic High Speed HDMI 0.5m Cables with Ethernet are 0.5m suitable for use with HDMI.
Home Theater, 1m Games Consoles (including Nintendo Wii U), 1m Blu-rays, Cable or Satelite 1m boxes and Projectors.
HDMI Cables provides the most reliable digital signal transfer and the purest picture.
</div>
</html>
上面的代码匹配"0.5m, 1m, 1 metre"
字符串中的等,但它搜索一行中的所有出现,我想在模式的第一次出现时停止并在下一行继续。
有人可以帮忙吗?