-2

I need to match words in HTML but need to skip a tag (with specific class) and its contents. Example:

<p> There is my way <span class="abc"> way beyond someone </span></p>

I need to match only first way word. Till now i am using word boundary to select word. /\b(way)\b/ig but it fails in below scenario.

<p> There is nothing. <span class="abc"> Way beyond someone </span></p>

In this statement i don;t want to match anything because way is inside the span.abc element.

I have tried ^ operator but doesn't seems to work. i.e. /(\b)way(\b)^("abc">the)/ig

  • 2
    Possible duplicate of [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – esqew Apr 06 '20 at 14:48
  • 2
    Don't use regular expressions to parse HTML. – Barmar Apr 06 '20 at 14:48
  • @Barmar I know we should not use regular expression. But in some cases you have limited choices when you deal with huge data you choose every possible way to slow down the queries. Querying DOM is too slow and am well aware of theories since years, Please help in this case, I am stuck for 4 hours. – Rajan Verma Apr 06 '20 at 14:52
  • @esqew that question is filled with theories. I have tired most of those examples. It shouldn't be that complex. – Rajan Verma Apr 06 '20 at 14:59

2 Answers2

0

I don't know a way to do what you want to, but you could use one regex for all the matches, use another regex for the ones you want to discard, and then use only the matches in one match and not the other. In your example:

way

Matches all the times this word appears

<span.*?>(.|\n)*?way(.|\n)*?<\/span>

Matches all span elements. Then you cant get all matches for first regex wich weren't matched by the second one.

0

i tried it in another method.... it returns the matched string without the word "Way"....

var str="<p> There is nothing. <span class=\"abc\"> Way beyond someone 
</span></p>";
var x=str.split(" ")
//console.log(x);
for(var i=0;i<x.length;i++)
{
    //console.log(x[i])
    if(x[i]!="Way")
{
    console.log(x[i])

}

}

hope it is helpful...

Lakshmi Ram
  • 11
  • 1
  • 7
  • please read the question again. The presence of word inside that HTML should be skipped and NOT MATCHED. Your solution is just doing the opposite. – Rajan Verma Apr 06 '20 at 16:26
  • @RajanVerma-edited the answer ....hope this is what your are asked.... – Lakshmi Ram Apr 06 '20 at 19:20
  • Please read the question once again .Your solution also select these elements `

    ` and ``. I have mentioned that i need to SKIP a TAG (with specific class) and its CONTENTS.

    – Rajan Verma Apr 08 '20 at 06:43