JSFiddle - React, Tailwind, and code Playground

by prankol57

HTML

<pre id="output">
</pre>

JavaScript

var someString = 'This is a nice little string with <a target="_" href="/carSale/12/..">link1</a>. But there is more that we want to do with this. Lets insert another <a target="_" href="/carSale/13/..">link2</a> ';

var div = document.createElement("div");
div.innerHTML = someString;

// Get links
var links = div.querySelectorAll("a");
for (var i = 0; i < links.length; ++i) {
    var a = links[i];
    // If the link contains a href with desired properties
    if (a.href.indexOf("carSale") >= 0) {
        // Replace the element with text
        div.replaceChild(document.createTextNode(a.innerHTML), a);
    }
}

var resultString = div.innerHTML;

console.log(resultString);

document.querySelector("#output").innerHTML = resultString;