0
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?

OUTPUT: 2

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

Using RegEx for example:

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

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.