简体   繁体   English

如何将包含特定单词的所有标签与正则表达式匹配?

[英]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=""我试图在包含相同单词的 html 中找到所有<divs> ,它可能在class=""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.这四个<divs>来自不同的页面。

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.我需要找到所有包含“聊天”一词的<divs> (或其他标签)并删除它们。 For this I will use the function为此,我将使用 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.当涉及到id=""时,上面的 function 可以正常工作,因为它会找到包含在 id 属性引号中的所有内容,这是唯一的。

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.另一方面,当涉及到 class 时,function 不起作用,因为正如我所说,它会返回报价中包含的所有内容。

IE IE

"bloom.chat_inside_th.dark_yellow" “bloom.chat_inside_th.dark_yellow”

while the function would need to at least eliminate the spaces between the different classe.而 function 至少需要消除不同类之间的空格。

".bloom.chat_inside_th.dark_yellow" “.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"?有没有办法在搜索类时消除这些空格,或者更好的是,只找到包含“chat”一词的 class,如“chat_inside_th”?

You can use an attribute selector: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors您可以使用属性选择器: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

[id*="chat"] will match all id attributes which have chat in them [id*="chat"]将匹配所有包含聊天id属性

 $("#go").on("click",e => { $('[id*="chat"],[class*="chat"]').remove(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="chat_widget_th" class="bg-warning checkbox chat_open_ts">one</div> <div class="bloom chat_inside_th dark_yellow">two</div> <div id="opened_widget_chat" class="active show">three</div> <div class="chat_child modal show fade">four</div> <button id="go">go</button>

Regex should not be used to parse HTML.不应该使用正则表达式来解析 HTML。

Instead, you can use a DOMParser to parse the string , from which you can select all elements whose id or class attribute contain 'chat' and remove them.相反,您可以使用DOMParser解析字符串,您可以从中 select idclass属性包含'chat'所有元素并删除它们。

 const str = `<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>`; const parsed = new DOMParser().parseFromString(str, 'text/html'); parsed.body.querySelectorAll('[id*=chat],[class*=chat]').forEach(e => e.remove()) const res = parsed.body.innerHTML; console.log(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM