您现在的位置是:网站首页> 编程资料编程资料

基于正则表达式实现UL下LI的样式替换功能_正则表达式_

2023-05-25 352人已围观

简介 基于正则表达式实现UL下LI的样式替换功能_正则表达式_

本文实例讲述了基于正则表达式实现UL下LI的样式替换功能。分享给大家供大家参考,具体如下:

最先我想到是在UL下填充好在替换发觉结果差强人意,没有真正改变样式:

 $("#UlContent li").each(function (index) { // alert(index + ': ' + $(this).text()); var text = $(this).text(); var regExp = new RegExp($("#search_content").val(), 'g'); var newText = text.replace(regExp,"" + $("#search_content").val() + "");//将找到的关键字替换,加上highlight属性; $(this).text(newText);//更新文章; }); 

其实应该在填充进UL前进行替换:

 $("#search_content").keyup(function () { if(CheckChinese($("#search_content").val())) { $.ajax({ type: "POST", anync: true, url: "HelpCenterSuggestion.ashx", cache: false, dataType: "text", data: { m: $("#search_content").val() }, success: function (result) { $("#UlContent li").remove(); var regExp = new RegExp($("#search_content").val(), 'g'); var newText = result.replace(regExp,"" + $("#search_content").val() + "");//将找到的关键字替换,加上highlight属性; $("#UlContent").append(newText); } }); 

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

希望本文所述对大家正则表达式学习有所帮助。

-六神源码网