Javascript find all text except those in tag

I have a div, and this div can (or cannot) have html elements as children. With my javascript, i need to find all the occurrences of a word inside this div, except for those in the <a> tag.

For example:

<div id="dictionable"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br/><br/> <a href="#lorem">lorem</a> <br/><br/> <p>lorem</p> </div>

I tried with my ultra low capabilities to build a regex, failing miserably. So I googled and found this:

var pattern = new RegExp('(lorem)(?![^<]*>|[^<>]*</)', 'gim');

this regex finds every occurrence of "lorem" but not in EVERY tag. I just need to exclude only the A tag.

Could anyone help me?


No regex. Absolutely no regex. Nuh-uh. Nope.

var copy = document.getElementById('dictionable').cloneNode(true), links = copy.getElementsByTagName('a'), l = links.length, i; for( i=l-1; i>=0; i--) { // always work in reverse order when deleting stuff, it's safer! links[i].parentNode.removeChild(links[i]); } var result = copy.textContent || copy.innerText;



Using jquery its too simple

var $dictionable = $("#dictionable").clone(); $dictionable.find('a').remove();//This will remove all <a> tag $dictionable.text();//This will give all text


Since everything in an element is considered as an element by itself, you can simply iterate through the div's children.

Granted, it's not the shortest solution due to its validation, but it should be relatively fast.

var d = document.getElementById('dictionable'); var textcontent = ''; for (node in d.childNodes) { // accept only element (1), text (3) and non-link element if ((d.childNodes[node].nodeType != 1 && d.childNodes[node].nodeType != 3) || d.childNodes[node].nodeName == 'A') continue; textcontent = textcontent+d.childNodes[node].textContent }

This was you can even set the search inside the loop and narrow the results down to a single element level.


  • regex not working “within” javascript string
  • Segoe UI Light Font Not displaying properly in Google Chrome
  • Implementing a memoization function in Haskell
  • How should I store date/time objects in SQL?
  • How should email address opt-in implemented?
  • Parse a very large CSV dataset
  • What part of this RegEx is causing it to find no matches in IE7 and IE8?
  • Cannot get ComboBox selected item value
  • Running scripts from Perl CGI programs with root permissions
  • SQLite INSERT SELECT Query Results into Existing Table?
  • Removing the last digits in string
  • sending a soap request from python
  • Add Special Case to Power BI Query (M)
  • How to hide header on specific page in wordpress
  • .NET RIA Services and Custom Data Model - CRUD Capabilities
  • Why is this generic method unsafe?
  • How can one copy the internal state of zlib compressor object in Python
  • How to get all use statements declared in PHP class file
  • Mysql stored procedure error
  • javascript regexp - replace all floating point numbers in a string with rounded numbers
  • Where can I get runkit DLL extensions for PHP 5.3+?
  • How do I run my Specs with the previous version of Rspec?
  • How to read JSON-LD data from HTML in Objective-C?
  • CUDA Debugging - VS on windows workstation, GPUs on Linux server?
  • JQuery form validation and submit scripts conflict
  • MAVEN : Run Multiple Maven Project using Maven Test
  • How can I prevent the need to copy strings passed to a avr-gcc C++ constructor?
  • Examples of how to a STS in .Net 4.5 using WCF
  • How to log in to an HTTPS website with Jsoup?
  • Why use database factory in asp.net mvc?
  • WinForms: two way TextBox problem
  • Convert array of 8 bytes to signed long in C++
  • R: gsub and capture
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Net Present Value in Excel for Grouped Recurring CF