简体   繁体   English

RegExp-在搜索中替换`html`标记并突出显示

[英]RegExp - replace the `html` tags on search and highlight

I am doing a search and highlight process using regex , the problem i am facing is, it's accounts the html tags too search option. 我正在使用regex进行搜索和突出显示过程,我面临的问题是,它也是html标签的搜索选项。

since the html element replaces, i am getting bad result. 由于html元素替换,我得到了不好的结果。 how to avoid this? 如何避免呢?

here is my patter to search and replace the html text to mark tag. 这是我的搜索和替换html文本mark标记的模式。

 var src_str = $("#test").html(); var origional = $("#backup").html(); var searchIt = function (text) { console.log(text); var marks = text; marks = marks.replace(/(\\s+)/,"(<[^>]+>)*$1(<[^>]+>)*"); var pattern = new RegExp("("+marks+")", "gi"); src_str = src_str.replace(pattern, "<mark>$1</mark>"); src_str = src_str.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\\/mark>)/,"$1</mark>$2<mark>$4"); $("#test").html(src_str); } var resetIt = function (Text) { $("#parent").html(origional); if(!Text.length) return; searchIt(Text); } $('#keyWord').keyup(function (e){ var inputText = $.trim($(e.target).val()); resetIt(inputText); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="parent"> <div id="test"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div> </div> <script type="text/template" id="backup" > <div id="test"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div> </script> <input type="text" id="keyWord" /> <button>Reset </button> 

So it seems like your problem is that you're inserting <mark>...</mark> tags around your search results to highlight them and later matches can't match around these as well? 因此,看来您的问题是您正在搜索结果中插入<mark>...</mark>标记以突出显示它们,而以后的匹配也无法在这些结果之间进行匹配?

I was able to get it working like this: 我能够使它像这样工作:

 var src_str = $("#test").html(); var origional = $("#backup").html(); var searchIt = function (text) { console.log(text); var marks = text; marks = marks.replace(/(\\s+)/,"(<[^>]+>)*$1(<[^>]+>)*"); var pattern = new RegExp("("+marks+"+(?=[^>]*(?:<|\\Z)))", "gi"); src_str = src_str.replace(/<\\/?mark.*?>/gi,"") src_str = src_str.replace(pattern, "<mark>$1</mark>"); src_str = src_str.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\\/mark>)/,"$1</mark>$2<mark>$4"); $("#test").html(src_str); } var resetIt = function (Text) { $("#parent").html(origional); if(!Text.length) return; searchIt(Text); } $('#keyWord').keyup(function (e){ var inputText = $.trim($(e.target).val()); resetIt(inputText); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="parent"> <div id="test"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div> </div> <script type="text/template" id="backup" > <div id="test"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div> </script> <input type="text" id="keyWord" /> <button>Reset </button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM