user986959
user986959

Reputation: 630

Jquery count specific character in div

I have a php code that generates some <a> tags and the final output looks like:

<div id='link1'>
<a style="font-size:10pt; color: #008056" href="somelink">Name</a>
<b>;</b>
</div>

My question is how can I count all of the semicolons in that div with jquery?

Upvotes: 4

Views: 2854

Answers (2)

Milind Anantwar
Milind Anantwar

Reputation: 82251

To get including those in the style attribute,Try this:

  alert($('#link1').html().split(";").length - 1);

And to get without one that are there in attributes:

  alert($('#link1').html().replace(/(<([^>]+)>)/ig,"").split(";").length - 1);

Working Demo

Update: To find one occurence of ; in a and remove it:

$('#link1 a').each(function(){
 if($(this).html().split(";").length==2){
  $(this).html($(this).html().replace(";",""))
}});

Working Demo

Upvotes: 5

Amit Joki
Amit Joki

Reputation: 59292

I don't know what you are up to, but if you need it, then you can do:

$('#link1').html().match(/;/g).length

/;/g is the regex that will match all ;

Upvotes: 5

Related Questions