How to match all tags containing specific word with regex?


I’m trying to find all <divs> in an html containing the same word, it could be either in the class="" or in the id=""

Example:

<div id="chat_widget_th" class="bg-warning checkbox chat_open_ts">...</div>
<div class="bloom chat_inside_th dark_yellow>...</div>
<div id="opened_widget_chat" class="active show">...</div>
<div class="chat_child modal show fade">...</div>

These four <divs> are from different pages.

They all correspond to a chat popup that i need to exclude. All of them contain, in some way, the word "chat".

I need to find all the <divs> (or other tags) that contain the word "chat" and delete them.
For this I will use the function

<script>
var regexclass = /class="K[^"]*?chat[^"]*?(?=")/;
var regexid = /id="K[^"]*?chat[^"]*?(?=")/;
$('#regexclass').remove();
$('#regexid').remove();
</script>

The above function works correctly when it comes to id="", because it finds everything that is enclosed in the quotes of the id attribute, which is unique.

When it comes to a class, on the other hand, the function does not work, because it returns, as I said, everything that is enclosed in quotation.

I.E

"bloom .chat_inside_th .dark_yellow"

while the function would need to at least eliminate the spaces between the different classe.

".bloom.chat_inside_th.dark_yellow"

Is there any way to eliminate these spaces when searching for classes or, better yet, find exclusively the class that contains the word "chat" like "chat_inside_th"?

Source: JavaSript – Stack Overflow

November 30, 2021
Category : News
Tags: javascript | jquery | regex

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.