簡體   English   中英

在html字符串中找到許多標簽

[英]find a number of tags in html string

var findnumberoftags = "<p>hihih</p><h2></h2><li>one</li><li>two</li><p>iam the best</p><h1>yes</h1><h2></h2>"

我想在字符串中找到<h2>標簽的數量嗎? 我怎樣才能做到這一點?

OUTPUT: 2

將字符串設為jquery對象,然后可以使用jquery方法對標簽進行計數。

var findnumberoftags ="<p>hihih</p><h2></h2><li>one</li><li>two</li><p>iam the best</p><h1>yes</h1><h2></h2>";
alert($(findnumberoftags ).filter("h2").length)

小提琴

以RegEx為例:

console.log( findnumberoftags.match( /<h2>/g ).length );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM