DEV Community

loading...

Discussion on: How to remove all links in JavaScript

Collapse
shadowtime2000 profile image
shadowtime2000

Using Regex might be one of the worst ways to do this. A better way is to modify a DOM tree instead.

var elem = document.createElement("div");

elem.innerHTML = "ur text here";

Array.from(elem.children).forEach(child => {
    if (!(child.tagName === "a")) return;
    child.replaceWith(document.createTextNode(child.textContent));
})
Enter fullscreen mode Exit fullscreen mode
Collapse
giulia_chiola profile image
Giulia Chiola Author

Thank you @shadowtime2000 , your way is definitely better 💪🏻 I updated the post adding your solution. Thanks!