Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
var findnumberoftags = "<p>hihih</p><h2></h2><li>one</li><li>two</li><p>iam the best</p><h1>yes</h1><h2></h2>"
I want to find the numbers of <h2> tags in my string? How can i do that?
<h2>
OUTPUT: 2
Make the string as a jquery object, then you can use jquery methods to count the tags.
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)
Fiddle
Add a comment
Using RegEx for example:
console.log( findnumberoftags.match( /<h2>/g ).length );
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.