Search code examples
javascripthtmlregexregexp-replace

RegEx to only look at text inside HTML tags?


I recently started learning/using about RegEx.

Is there a way to avoid matching words that are HTML tag attributes or belonging to tag attributes?

For example:

<p style=“position: absolute”>position: </p>

I tried

/\bposition\b\W\s/g

But that matches both instances.

Can I only match the second “position: “?

Clarification:

I am trying to search the document for words that the user enters and replace them with a span element containing those words - this is similar to "Ctrl + F". Simply having the text is not enough as I would need a way to also update the document once the text was replaced with the span elements.


Solution

  • The RegEx to match the HTML / XML tags is /(<([^>]+)>)/ig. Maybe be this is what you're looking for.

    let str = '<p style="position: absolute">position: </p>';
    const strWithoutTag = str.replace(/(<([^>]+)>)/ig, '');
    console.log(strWithoutTag);