Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7031153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:45:56+00:00 2026-05-28T00:45:56+00:00

I want to replace a word that is in html content (not attributes) and

  • 0

I want to replace a word that is in html content (not attributes) and I’ve googled and found variant regular expressions, but none of them could not cover all cases.

for example replace “width” to “[width]”

<div style="width: 200px">
    width is 300.
    <div>
        <div style="width: 100px">
            width is 200.
        </div>
    </div>
</div>

to

<div style="width: 200px">
    [width] is 300.
    <div>
        <div style="width: 100px">
            [width] is 200.
        </div>
    </div>
</div>

Thanks in advance

Example Live example

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:45:57+00:00Added an answer on May 28, 2026 at 12:45 am

    As I already said in my comment, iterate over all text nodes and replace their content.

    function replaceText(element, pattern, replacement) {
    
        for(var child = element.firstChild; child; child = child.nextSibling) {
            if(child.nodeType === 3) {
                child.nodeValue = child.nodeValue.replace(pattern, replacement);
            }
            else if(child.nodeType === 1) {
                replaceText(child, pattern, replacement);
            }
        }
    
    }
    

    Assuming element is a reference to the root:

    replaceText(element, /(\b)width(\b)/g, '$1[width]$2');
    

    DEMO

    If you haven’t parsed the HTML yet, you can create a temporary element:

    var tmp_root = document.createElement('div');
    tmp_root.innerHTML = yourHTMLString;
    

    Something similar can be done with jQuery using .contents() [docs] to get all child nodes, including text nodes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse a random website, modify the content so that every word
I'm looking for a regular expressions string that can find a word or regex
I want to replace only the first occurrence of a word in a sentence
I have a template in word (.docx) format and want to replace some placeholders
I want to replace all mailto: links in html with plain emails. In: text
I want to replace word groups by links. I use a multi-dimensional array to
I am developing an application using PHP but I am new to regular expressions,
i want to replace some string(s) from a source string. so that shouldn't be
I'm new to regular expressions in Java (or any language, for that matter) and
i want to replace all between <p> tags with a simple word NotFound <p

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.