Javascript会查找除< a>之外的所有文本标签 [英] Javascript find all text except those in <a> tag
问题描述
我有一个div,并且这个div可以(或不能)具有html元素作为子元素。用我的javascript,我需要找到这个div内所有单词的出现,除了那些在< a>
标签中。
例如:
< div id =dictionable>
Lorem ipsum dolor sit amet,consectetur adipiscing elit。
< br />< br />
< a href =#lorem> lorem< / a>
< br />< br />
< p> lorem< / p>
< / div>
我试着用我的超低能力构建一个正则表达式,失败了。
所以我google了一下,发现这个:
var pattern = new RegExp('(lorem)(?![^ <] *> | [^> *< /)','gim');
这个正则表达式查找每个lorem的出现,但不是在每个标签中。
我只需要排除A标签。
任何人都可以帮我吗?
没有正则表达式。绝对没有正则表达式。新加坡国立大学医院,嗯。
pre $ var $ copy $ document $ get_lementById('dictionable')。cloneNode(true),
links = copy .getElementsByTagName('a'),l = links.length,i;
for(i = 1-1; i> = 0; i--){
//删除东西时总是按相反顺序工作,这样更安全!
links [i] .parentNode.removeChild(links [i]);
}
var result = copy.textContent || copy.innerText;
繁荣!
I have a div, and this div can (or cannot) have html elements as children. With my javascript, i need to find all the occurrences of a word inside this div, except for those in the <a>
tag.
For example:
<div id="dictionable">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<br/><br/>
<a href="#lorem">lorem</a>
<br/><br/>
<p>lorem</p>
</div>
I tried with my ultra low capabilities to build a regex, failing miserably. So I googled and found this:
var pattern = new RegExp('(lorem)(?![^<]*>|[^<>]*</)', 'gim');
this regex finds every occurrence of "lorem" but not in EVERY tag. I just need to exclude only the A tag.
Could anyone help me?
No regex. Absolutely no regex. Nuh-uh. Nope.
var copy = document.getElementById('dictionable').cloneNode(true),
links = copy.getElementsByTagName('a'), l = links.length, i;
for( i=l-1; i>=0; i--) {
// always work in reverse order when deleting stuff, it's safer!
links[i].parentNode.removeChild(links[i]);
}
var result = copy.textContent || copy.innerText;
Boom!
这篇关于Javascript会查找除< a>之外的所有文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!