Home > Web Front-end > JS Tutorial > Highlight characters using regular expressions

Highlight characters using regular expressions

php中世界最好的语言
Release: 2017-12-30 17:58:02
Original
2047 people have browsed it

这次给大家带来的是用正则表达式让字符高亮显示,把数字改换成正则表达式的中文或者指定的字符,既可以高亮。还可以设置其他的高亮颜色,这篇文章就给大家好好分析一下。

红色:style='background:red;'

蓝色:style='background:blue;'

绿色:style='background:green;'

<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);
Copy after login

相信看了以上介绍你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

相关阅读:

JS怎样可以做到点击跳转到登陆的个人邮箱

require.js的使用方法详细介绍

Ajax应该如何使用

The above is the detailed content of Highlight characters using regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template