简体   繁体   English

使用局部替换替换存储为字符串的html元素

[英]Replace html element stored as a string using partial

I'm using node.js to pull in html from an API, I'm storing it in a variable before I display it. 我正在使用node.js从API中提取html,在将其显示之前将其存储在变量中。 I need to replace a link in that html string but I'm only able to use the front part of the link to search, as they will be dynamic. 我需要替换该html字符串中的链接,但我只能使用链接的前部进行搜索,因为它们将是动态的。

I found an example that would work great using document.querySelectorAll("a[href^='http://somelink.com/12345678']") 我找到了一个使用document.querySelectorAll("a[href^='http://somelink.com/12345678']")

Javascript getElement by href? Javascript getElement通过href?

But I'm not using the DOM. 但是我没有使用DOM。

Dynamic Links that need to be removed/replaced: 需要删除/替换的动态链接:

<a href="http://somelink.com/12345678-asldkfj>Click Here</a>
<a href="http://somelink.com/12345678-clbjj>Click Here</a>
<a href="http://somelink.com/12345678-2lksjd>Click Here</a>

What I can search on: 我可以搜索的内容:

<a href="http://somelink.com/12345678

I need to either change the actual link name "Click Here" or remove the element. 我需要更改实际的链接名称“ Click Here”或删除该元素。

Any ideas how to achieve this with plain JS? 有什么想法如何用普通的JS实现吗? Initially I'm thinking maybe there is a way to create a fake/temp DOM? 最初,我在想也许有一种创建伪/临时DOM的方法吗?

EDIT: Modifying the answer below with my code, it did exactly what I needed. 编辑:修改下面的答案与我的代码,它正是我所需要的。

var str = '<a href="http://somelink.com/12345678-asldkfj">Click Here</a><a href="http://somelink.com/12345678-clbjj">Click Here</a><a href="http://somelink.com/12345678-2lksjd">Click Here</a>';
var div = document.createElement("div");
div.innerHTML = str;

var links = div.querySelectorAll("a[href^='http://somelink.com/12345678']");

for(i=0; i<links.length; i++) {
    if(links[i]) {
        str = str.replace(links[i].outerHTML, 'New Name');
  }
}

console.log(str);

You don't get nothing because your links href attribute are not correctly ended, there's a missing " at the end, if you fix it, everything will be fine. 您没有得到任何东西,因为您的links href属性未正确结束,结尾处缺少" ,如果您进行了修复,一切都会好起来的。

Otherwise if you are'nt using HTML and DOM, you can append your HTML string in a temporary DOM element like this: 否则,如果您不使用HTML和DOM,则可以将HTML字符串附加到临时DOM元素中,如下所示:

 var str = ' <a href="http://somelink.com/12345678-asldkfj">Click Here</a>' +'<a href="http://somelink.com/12345678-clbjj">Click Here</a>' + '<a href="http://somelink.com/12345678-2lksjd">Click Here</a>'; var div = document.createElement("div"); div.innerHTML = str; var links = div.querySelectorAll("a[href^='http://somelink.com/12345678']"); console.log(links); 

Note: 注意:

To use this code in a nodejs environment you will need to use a DOM parser module , these are some modules that can help you: 要在nodejs环境中使用此代码,您将需要使用DOM解析器模块 ,这些模块可以为您提供帮助:

A fake dom would be serious over-kill here. 伪造的dom在这里会严重毁灭。 All you need is a string replace. 您只需要一个字符串替换即可。 If you know for sure that your strings are safe, then this example should be sufficient. 如果您确定您的字符串是安全的,那么此示例就足够了。

Edit : Added parsing an html string to generate the array of links to work on and added replacement of innerText. 编辑 :添加了解析html字符串以生成要处理的链接数组,并添加了innerText的替换。

To get an array of links from an html string: 要从html字符串获取链接数组:

  • Match <a , followed by 0 or more not > , followed by > , followed by shortest possible string to rest of the match, followed by </a> 比赛<a其次是0个或多个不> ,其次是> ,其次是最短的字符串匹配的休息,随后</a>

  • This pattern includes capture groups for the opening/closing tags because then we can reuse the same pattern to replace the anchor's innerText later. 该模式包括用于开始/结束标签的捕获组,因为这样我们以后可以重用相同的模式来替换锚点的innerText。

To replace the href of each link: 替换每个链接的href:

  • Match href=" , followed by 1 or more not " , followed by " 匹配href=" ,后接1个或多个非" ,后接"
  • Replacing the full match with href=" , followed by new url, followed by " . href="替换完整匹配,后跟新url,后跟"

To replace innerText of anchor: 替换锚的innerText:

  • Match ( <a , followed by 0 or more not > , followed by > ), followed by shortest possible string to rest of the match, followed by( </a> ), capturing the opening tag in $1 and closing tag in $3. 匹配( <a接着0或多个不> ,随后> ),其次是最短的字符串匹配的休息,然后( </a> ),捕捉开始标记$ 1和$在3关闭标记。
  • Replace string with opening tag, followed by new text, followed by closing tag. 将字符串替换为开始标签,新文本和结束标签。

 const linksHtml = document.querySelector('#links').innerHTML // Note that capture group 2 will not actually capture "shortest string" even // though it matches. $2 in a replace() would return huge useless string. const anchorPattern =/(<a[^>]*>)(.*?)(<\\/a>)/g const links = linksHtml.match(anchorPattern) const newUrls = [ 'http://someotherlink.com/cool', 'http://someotherlink.com/happy', 'http://someotherlink.com/smile' ] const newText = [ 'Cool', 'Happy', 'Smile', ] const replaced = links // replace urls .map( (link, i) => link.replace(/(href=")[^"]+"/, `$1${newUrls[i]}"`) ) // replace innerText .map( (link, i) => link.replace(anchorPattern, `$1${newText[i]}$3`) ) document.querySelector('pre') .innerText = JSON.stringify(replaced,null,2) 
 <div id="links"> <h2>Probably will be a header.</h2> <a href="http://somelink.com/12345678-asldkfj">Click Here</a> <p>And maybe some random text.</p> <a href="http://somelink.com/12345678-clbjj">Click Here</a> <p>One of the links might be in a paragraph. <a href="http://somelink.com/12345678-2lksjd">Click Here</a></p> </div> <h2>Result: </h2> <pre/> 

You could use string searching or regular expressions ( but shouldn't unless extremely simple html ) to try to manipulate your html string. 您可以使用字符串搜索或正则表达式( 但除非非常简单的html,否则不应该 )来尝试操作html字符串。 But you can, and would be easier to, import packages that create DOM parsing / manipulation methods, like Cheerio (jQuery like) or jsDOM . 但是您可以并且会更容易地导入创建DOM解析/操作方法的包,例如Cheerio (类似于jQuery)或jsDOM

From there you would parse the string into a DOM document, do the querying and replacing the text or removing the elements through their methods. 从那里,您可以将字符串解析为DOM文档,进行查询和替换文本,或通过其方法删除元素。

jsDOM Example: jsDOM示例:

const JSDOM = require("jsdom");
const dom = new JSDOM(yourHtmlString);
const document = dom.window.document;

var elements = document.querySelectorAll("a[href^='http://somelink.com/12345678']");

for(let i=0; i<elements.length; i++){
  elements[i].textContent = "Replacement text";
  //element.remove() if removing
}

var resultHtml = dom.serialize();

Cheerio Example: Cheerio示例:

var cheerio = require('cheerio');
$ = cheerio.load(yourHtmlString);

$("a[href^='http://somelink.com/12345678']").text('Text to Replace "Click Here"');
//or .remove() if wanting to remove

var htmlResult = $.html();

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

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