Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Regex to select every text word that is text but not HTML or code

Trying to find all words within a specific tag like all p tags, so that I can apply some regex to it and replace all the words.

For example,

<p>the lazy cat went <div>up</div> to the cheery fox</p>

You can choose an "all words" regex:

/\w+/g

You can use a jquery replace. Here's my code that not fully working yet.

$("p").html($("p").html().replace(/\w+/g, 'fox'));

Now what I need to do is get it to work, so that all the words inside the p tag are replaced with another word. Result:

<p>fox fox fox fox <div>fox</div> fox fox fox fox</p>

How can I get jquery to replace all individual words that are inside a p tag, but not affect any other HTML or code inside it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Retrieve all text nodes instead, and iterate over them to reassign their content to the replaced string.

const getAllTextNodes = (parent) => {
    const walker = document.createTreeWalker(
        parent,
        NodeFilter.SHOW_TEXT,
        null,
        false
    );
    let node;
    const textNodes = [];
    while(node = walker.nextNode()) {
        textNodes.push(node);
    }
    return textNodes;
};
for (const p of $('p')) {
  for (const textNode of getAllTextNodes(p)) {
    textNode.textContent = textNode.textContent.replace(/\w+/g, 'fox');
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>the lazy cat went up to the cheery fox</p>

Or ditch jQuery, since it's not really providing any benefit

const getAllTextNodes = (parent) => {
    const walker = document.createTreeWalker(
        parent,
        NodeFilter.SHOW_TEXT,
        null,
        false
    );
    let node;
    const textNodes = [];
    while(node = walker.nextNode()) {
        textNodes.push(node);
    }
    return textNodes;
};
for (const p of document.querySelectorAll('p')) {
  for (const textNode of getAllTextNodes(p)) {
    textNode.textContent = textNode.textContent.replace(/\w+/g, 'fox');
  }
}
<p>the lazy cat went up to the cheery fox</p>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!