Home > Article > Web Front-end > Examples of regular expressions in JavaScript that highlight numbers, Chinese characters, and specified characters
这篇文章主要介绍了JavaScript中正则表达式使数字、中文或指定字符高亮显示,需要的朋友可以参考下
<span id="span_id">span2314的23文本213</span> var htmlobj = txt.replace(/(\d+)/img, "<span style='background:red;'>$&</span>"); //var htmlobj = txt.replace(/([+\-]?[0-9]+(\.[0-9]+)?)/g, "<span style='background:red;'>$1</span>"); // 这个也可以 //var htmlobj = txt.replace(/(\d+)/img, "<span style='background:yellow;'>$1</span>");//这个也可以 alert("替换spna中的数字:"+htmlobj); $("#span_id").html(htmlobj);
把数字改换成正则表达式的中文或者指定的字符,既可以高亮。还可以设置其他的高亮颜色:
红色:style='background:red;'
蓝色:style='background:blue;'
绿色:style='background:green;'
The above is the detailed content of Examples of regular expressions in JavaScript that highlight numbers, Chinese characters, and specified characters. For more information, please follow other related articles on the PHP Chinese website!