简体   繁体   English

Javascript:replace()除了html标签之外的所有内容

[英]Javascript: replace() all but only outside html tags

I have an autocomplete form and when showing the results matching the user's search string, I want to highlight the search string itself. 我有一个自动完成表单,当显示与用户的搜索字符串匹配的结果时,我想突出显示搜索字符串本身。 I plan to do this by wrapping any occurrence of the search string within a tag such as , or a with a given class. 我计划通过将任何匹配的搜索字符串包含在标记中来实现,或者使用给定的类。 Now, the problem is that when using regEx I have problems if the pattern occurs within a html tag. 现在,问题是当使用regEx时,如果模式出现在html标记内,我会遇到问题。 For instance 例如

var searchPattern = 'pa';
var originalString = 'The pattern to <span class="something">be replaced is pa but only outside the html tag</span>';

var regEx = new RegExp(searchPattern, "gi")

var output = originalString.replace(regEx, "<strong>" + searchPattern + "</strong>");

alert(output);

(Demo: http://jsfiddle.net/cumufLm3/7/ ) (演示: http//jsfiddle.net/cumufLm3/7/

This is going to replace also the occurrence of "pa" within the tag 这也将取代标签内“pa”的出现

 <span class="something">

breaking the code. 打破代码。 I'm not sure how to deal with this. 我不知道如何处理这件事。 I've been checking various similar questions, and I've understood that in general I shouldn't use regular expressions to parse html. 我一直在检查各种类似的问题,我已经明白,一般来说我不应该使用正则表达式来解析html。 But I'm not sure if there is any quick way to parse smoothly the html string, alter the text of each node, and "rebuild" the string with the text altered? 但我不确定是否有任何快速方法可以平滑地解析html字符串,更改每个节点的文本,并“重建”字符串并更改文本?

Of course I suppose I could use $.parseHTML(), iterate over each node, and somehow rewrite the string, but this seems to me to be too complex and prone to errors. 当然我想我可以使用$ .parseHTML(),迭代每个节点,并以某种方式重写字符串,但在我看来这太复杂,容易出错。 Is there a smart way to parse the html string somehow to tell "do this only outside of html tags"? 是否有一种聪明的方法来解析html字符串以某种方式告诉“只在html标签之外做这个”?

Please notice that the content of the tag itself must be handled. 请注意,必须处理标签本身的内容。 So, in my example above, the replace() should act also on the part "be replaced is pa but only outside the html tag". 因此,在上面的示例中,replace()也应该在“被替换为pa但仅在html标记之外”的部分上起作用。

Any idea of either a regular expression solid enough to deal with this, or (better, I suppose) to elegantly handle the text parts within the html string? 任何一个正则表达式足以解决这个问题的想法,或者(更好的,我想)优雅地处理html字符串中的文本部分?

Your code should look like this: 您的代码应如下所示:

var searchWord = 'pa';
var originalString = 'The pattern to <span class="something">be replaced is pa but only outside the html tag</span>';

var regEx = new RegExp("(" + searchWord + ")(?!([^<]+)?>)", "gi");

var output = originalString.replace(regEx, "<strong>$1</strong>");

alert(output);

Source: http://pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/ 资料来源: http//pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/

Parse the HTML and find all text nodes in it, doing the replace in all of them. 解析HTML并查找其中的所有文本节点,在所有节点中进行替换。 If you are using jQuery you can do this by just passing the snippet to $() which parses it in a Document Fragment, which you can then query or step over all elements and find all the .text() to replace. 如果您正在使用jQuery,您可以通过将代码段传递给$()来完成此操作, $()在文档片段中解析它,然后您可以查询或跳过所有元素并找到要替换的所有.text()

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

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