How do I remove a certain text element from a div using jQuery if it is only appears a certain number of times?

I have a dynamically generated div that looks like (two random examaples shown below):

<div class="col-lg-6 col-sm-6 col-xs-12" id="association_content">
  <img src="http://www.missionfamily.org/image/cache/catalog/marchio_fas-270x269.jpg">
  <!--<img src="image/cart-ban.png"/>-->
  <h1>FAS - Fondo Ambiente Sammarinese</h1>
  <br>	
  &nbsp;&nbsp;|&nbsp;&nbsp;<strong><i class="fa fa-envelope-o"></i></strong> <a href="mailto:info@fondoambiente.sm?Subject=Messaggio%20da%20Mission%20Family:%20">info@fondoambiente.sm</a>	
</div>

Open in new window


or

<div class="col-lg-6 col-sm-6 col-xs-12" id="association_content">
  <img src="http://www.missionfamily.org/image/cache/catalog/MarchioPolisportivaStella-270x269.gif">
  <!--<img src="image/cart-ban.png"/>-->
  <h1>Polisportiva Stella S.G.</h1>
  Via Abruzzo, 36 47923 Rimini RN<br>	
  <strong><i class="fa fa-phone"></i></strong> 0541781283 &nbsp;&nbsp;|&nbsp;&nbsp;<i class="fa fa-mobile"></i> 0541781283	
  &nbsp;&nbsp;|&nbsp;&nbsp;<strong><i class="fa fa-envelope-o"></i></strong> <a href="mailto:segreteria@polstella.com?Subject=Messaggio%20da%20Mission%20Family:%20">segreteria@polstella.com</a>	
</div>

Open in new window




What I need to do is check if the string &nbsp;&nbsp;|&nbsp;&nbsp; appears more than once or not. If it appears ONLY ONCE, then I need to remove it, otherwise it can stay as it is. I need to remove just that string leaving the rest of the DIV as it is.

I've been trying to detect this using: $('#association_content').text().indexOf('&nbsp;&nbsp;|&nbsp;&nbsp;')
but I keep getting -1 in the console.log

thanks in advance
badwolfffAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Instead of .text() use .html()
Julian HansenCommented:
This JQuery snippet should do it
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  // GET THE TEXT FROM THE DIV
  var divtxt = $('#association_content').html();
  
  // MATCH THE SEARCH STRING GLOBALLY
  var matches = divtxt.match(/&nbsp;&nbsp;\|&nbsp;&nbsp;/g);

  // IF MATCHES LENGTH IS 1 THEN WE HAVE A CANDIDATE 
  // FOR PRUNING
  if (matches.length == 1) {
    // REPLACE THE STRING WITH THE EMPTY STRING
    divtxt = divtxt.replace(/&nbsp;&nbsp;\|&nbsp;&nbsp;/,'');

    // FINALLY PUT THE AMENDED TEXT BACK IN THE DIV
    $('#association_content').html(divtxt);
  }
});
</script>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.