Home  >  Article  >  Web Front-end  >  Examples of regular expressions in JavaScript that highlight numbers, Chinese characters, and specified characters

Examples of regular expressions in JavaScript that highlight numbers, Chinese characters, and specified characters

黄舟
黄舟Original
2017-10-31 09:35:301341browse

这篇文章主要介绍了JavaScript正则表达式使数字、中文或指定字符高亮显示,需要的朋友可以参考下

<span id="span_id">span2314的23文本213</span>
var htmlobj = txt.replace(/(\d+)/img, "<span style=&#39;background:red;&#39;>$&</span>");
//var htmlobj = txt.replace(/([+\-]?[0-9]+(\.[0-9]+)?)/g, "<span style=&#39;background:red;&#39;>$1</span>"); // 这个也可以
//var htmlobj = txt.replace(/(\d+)/img, "<span style=&#39;background:yellow;&#39;>$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn