为什么将.filter()与.match()一起使用仅返回与条件匹配的第一个元素?
我有一些HTML代码,其中最嵌套的级别有一些我感兴趣的文本:
<div class="main">
<div class="container">
<div class="output_area">
<pre>WHITE 34</pre>
</div>
<div class="output_area">
<pre>RED 05</pre>
</div>
<div class="output_area">
<pre>WHITE 16</pre>
</div>
<div class="output_area">
<pre>BLACK</pre>
</div>
</div>
</div>
我需要做的是,我需要仅在output_area
元素元素汇回它们的嵌套&lt; pre&gt; pre&gt;元素包含一个单词 +一个数字(例如白色05,而不仅仅是黑色)。
因此,这就是我所做的:
我从所有output_area元素中制作了一个数组:
output_areas = Array.from(document.getElementsByClassName('output_area'));
我将output_areas
数组过滤到仅返回那些output> output_area
elements elements nested&lt; pre&gt; pre&gt;满足我的单词 +一个数字的状况,使用regexp,就像这样:
output_areas.filter(el => el.textContent.match(/^WHITE \d+$/g));
现在,发生的事情是,此函数只会返回第一个匹配结果,因此我将获得一个长度为1的对象,其中包含1 :
<div class="output_area">
<pre>WHITE 34</pre>
</div>
和output_area
元素包含&lt; pre&gt; 就可以看到“白色16”。
您在正则表达式的末尾看到的是,我将“ G”索请求全局搜索,而不仅仅是在第一个结果停止,
不明白为什么这不起作用,我试图验证如果我使用Include()执行搜索会发生什么:(
output_areas.filter(el => el.textContent.includes('WHITE')
让我们现在忘记数字,这并不重要)
,发生了什么? 这也将仅返回第一个output_area
...
,但是为什么???我在做什么错? 我并不是为了说过去几个小时我一直在为此敲打我而感到羞耻……而在这一点上,我只想了解什么是不起作用的。
我认为我得到的唯一线索是,如果我仅使用a ==或!=简化搜索,例如:
output_areas.filter(el => el.textContent != "")) // return all not empty elements
我恢复了所有output_area元素,而不仅仅是第一个元素!
因此,我怀疑使用过滤器()&amp; match()或filter()&amp;包括(),但与我的Google搜索有关,我没有将我带到任何地方...
所以我希望您能提供帮助!
I have some HTML code where at the most nested level there is some text I'm interested in:
<div class="main">
<div class="container">
<div class="output_area">
<pre>WHITE 34</pre>
</div>
<div class="output_area">
<pre>RED 05</pre>
</div>
<div class="output_area">
<pre>WHITE 16</pre>
</div>
<div class="output_area">
<pre>BLACK</pre>
</div>
</div>
</div>
What I need to do is I need to return the output_area
elements only when their nested <PRE> element contains a word + a number (for example WHITE 05, and not just BLACK).
So this is what I did:
I made an array from all output_area elements:
output_areas = Array.from(document.getElementsByClassName('output_area'));
I filtered the output_areas
array to only return those output_area
elements whose nested <PRE> satisfies my condition of a word + a number, using a regexp, like so:
output_areas.filter(el => el.textContent.match(/^WHITE \d+$/g));
Now, what happens is this function will only return the first matching result, so I will get an object of length 1 containing just :
<div class="output_area">
<pre>WHITE 34</pre>
</div>
and the output_area
element containing <PRE> with "WHITE 16" is not returned.
As you can see at the end of the regular expression I put a "g" to request a global search and not just stop at the first result.
Not understanding why this did not work, I tried to verify what would happen if I would use includes() to perform a search:
output_areas.filter(el => el.textContent.includes('WHITE')
(let's just forget about the numbers now, it's not important)
And what happens? This will also return only the first output_area
...
But why??? What am I doing wrong?
I am not ashamed to say I've been banging my head on this for the last couple of hours... and at this point I just want to understand what is not working.
The only clue I think I got is that if I simplify my search using just a == or !=, for example:
output_areas.filter(el => el.textContent != "")) // return all not empty elements
I get back all output_area elements and not just the first one!
So I suspect there must be some kind of problem when using together filter() & match(), or filter() & includes(), but with relation to that my google searches did not take me anywhere useful...
So I hope you can help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用在此处删除文本之前和之后的空间
You should use
trim
here to remove space before and after the text因为出于某种原因,它开始工作而没有我身边的任何更改...是的,只是我们都知道的典型IT案例之一... :)
回答自己, )卡住了...
可能没有我注意到的jupyter运行时间(正在为页面上服务)崩溃了,这在某种程度上导致了我正在寻找的那种不一致的情况。
故事的寓意:如果您看到与Python笔记本的互动中的怪异行为,请务必检查Jupyter运行时状态,然后愚蠢地尝试解决不可能的错误。
Answering myself as for some reason it then begin to work without any changes from my side... Yes, just one of those typical IT cases we all know... :)
Jokes aside, I think for some reason the webpage (the DOM) got stuck...
Probably the Jupyter Runtime (which was serving the page) had crashed without me noticing, and this caused somehow the kind of inconsistency I was looking at.
Moral of the story: if you see weird behaviour in the interaction with a Python Notebook, always go check the Jupyter Runtime status before getting stupid at trying to fix impossible errors.
我不确定jupyter笔记本电脑的问题是什么,但总的来说 - 仅基于问题中的HTML - 我相信您想使用XPATH而不是CSS选择器来实现您要做的事情:
输出应该是3
div
符合条件。I'm not sure what the issue with the Jupyter notebooks is, but generally speaking - based only on the HTML in the question - what I believe you are trying to do can be achieved using xpath instead of css selectors:
The output should be the 3
div
s meeting the condition.