简体   繁体   English

在不影响标签的情况下替换html中的所有单词

[英]Replace all words in html without affecting tags

I am looking for a way to replace all instances of Foo with <a>Bar</a> (a functioning link) in a given website.我正在寻找一种方法来将给定网站中的所有Foo实例替换为<a>Bar</a> (一个功能链接)。 Ideally without any extra library like jquery.理想情况下没有任何额外的库,如 jquery。

What I tried:我试过的:

  • document.body.innerHtml and similars ( document.getElementByTagName('p) ) won't work with a simple .replace(/Foo/g, '<a>Bar</a>') because it may end up replacing a Foo class-name or id, so it breaks the page. document.body.innerHtml和类似的document.getElementByTagName('p)document.getElementByTagName('p) )不适用于简单的.replace(/Foo/g, '<a>Bar</a>')因为它最终可能会替换一个Foo类名或 id,所以它打破了页面。 Also may affect some js scripts in the body.也可能会影响 body 中的一些 js 脚本。
  • Editing only textNodes (with something like this ) does not work because it displays the a tags as text, instead of creating DOM elements.编辑只textNodes(与像这样,因为它显示)不起作用a ,而不是创建DOM元素的标签为文本。
  • Trying to solve the textNodes problem and going to the parent element and applying .replace there just generates the same problem as the first bullet.尝试解决 textNodes 问题并转到父元素并在那里应用.replace只会产生与第一个项目符号相同的问题。

I've searched for answers to other similar questions but I haven't found any that solves this three points.我已经搜索了其他类似问题的答案,但没有找到解决这三点的答案。

You're close, but you need to replace the entire text node with a set of three new nodes:你已经接近了,但你需要用一三个新节点替换整个文本节点:

  1. a textnode that contains "everything before your string",一个包含“字符串之前的所有内容”的文本节点,
  2. an HTML node for the tag you want (and don't use <a> unless it's an actual link), and then你想要的标签的 HTML 节点(不要使用<a>除非它是一个实际的链接),然后
  3. another textnode that contains all the remaining text.另一个包含所有剩余文本的文本节点。

And then, of course, that remaining textnode is now part of the still-unprocessed set of text nodes.然后,当然,剩余的文本节点现在是尚未处理的文本节点集的一部分。

(unless you do a split for your term, then interleave as many additional nodes as needed. Then you just run through textnodes one by one without having to put "the remainder" back on your stack) (除非你对你的任期进行split ,然后根据需要交错尽可能多的额外节点。然后你只需一个一个地运行文本节点,而不必将“剩余部分”放回堆栈中)

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

相关问题 替换 div 内的文本而不影响其中的任何 HTML 标签 - Replace text inside a div without affecting any HTML tags inside of it 如何在不影响使用 Nodejs 或 Javascript 的 HTML 标签的情况下从 HTML 中获取 100 到 200 个单词? - How can I get 100 to 200 words from HTML without affecting HTML tags using Nodejs or Javascript? javascript - 用html标签替换所有空格以包含剩余的单词 - javascript - replace all white spaces with html tags to enclose remaining words 替换HTML中的所有单词 - Replace ALL words in HTML 在JavaScript中,如何在不影响标记的情况下替换HTML页面中的文本? - In JavaScript, how can I replace text in an HTML page without affecting the tags? 如何替换包含HTML标签的单词而不丢失HTML标签? - How can I replace words that contain HTML tags without losing the HTML tags? 使用javascript正则表达式获取没有html标签的所有单词 - get all words without html tags with javascript regex jQuery-用标记替换文本而不影响HTML - Jquery - Replace text with tokens without affecting the HTML 替换几乎所有 HTML 标记,而不用 JS 字符串中的一些标记 - Replace almost all HTML tags without some of them in string in JS 如何将 \\n 替换为<br />不影响其他标签? - How to replace \n with <br /> without affecting other tags?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM