简体   繁体   中英

Javascript regex : remove text between HTML tags

i want to remove text that is between any HTML tags :

example :

<div>
   <h1>Title</h1>
</div>

my var result should be :

<div>
    <h1></h1>
</div>

If, as your question suggests, you want to remove all text from between any HTML tags… only the real DOM is going to cut it.

function removeAllTextNodes(node) {
    if (node.nodeType === 3) {
        node.parentNode.removeChild(node);
    } else if (node.childNodes) {
        for (var i = node.childNodes.length; i--;) {
            removeAllTextNodes(node.childNodes[i]);
        }
    }
}

This, unlike textContent and innerHTML , will keep all existing element structure in place and remove only text.

If you really have a string and are using client-side JavaScript in a browser, and the string represents part of a document's content (and not an entire document – ie you won't find any DTD, <html> , <head> , or <body> elements within), then you can parse it just by putting it into an element:

var container = document.createElement("div");
container.innerHTML = htmlString;
removeAllTextNodes(container);
return container.innerHTML;

Otherwise, you'll probably want an HTML parser for JavaScript. Regular expressions, as it's been noted, aren't great at parsing HTML.

VANILLA JS TO THE RESCUE

var x = document.getElementsByTagName("h1");
for (var i=0; i<x.length; i++) {
    x[i].innerHTML = "";
}

Just insert any tag you'd like and wallah, no need for regex, or a 90kb library.

Javascript is already able to accomplish this with built in functions in a way that in conceptually superior to regex

<div>
   <h1 id="foo">Title</h1>
</div>
<script>
   document.getElementById("foo").textContent = ""
</script>

You would probably want to do something like this;

var elements = document.getElementsByTagName('*');
for(var i = 0; i < elements.length; i++) {
    var element = elements[i];
    if(element.children.length === 0) {
        elements[i].textContent = '';
    }
}

This

  • Finds all elements
  • Loops through them
  • Removes any text content

Docs:

You can also make this re-usable like so

var removeAllText = function() {
    var elements = document.getElementsByTagName('*');
    for(var i = 0; i < elements.length; i++) {
        var element = elements[i];
        if(element.children.length === 0) {
            elements[i].textContent = '';
        }
    }
}

Then whenever you want you can do this

removeAllText();

Don't use regex. Use something like loadXMLDoc() to parse the DOM and print the tags, instead of trying to remove the values from within the tags.

测试了我的 JS 并为我工作:

String.replace(/<yourtag>[\s\S]*<\/yourtag>/g, ""); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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