Q:

JQuery - Replace Underscore With Space

I have the following html and I want to replace underscores with spaces for all anchor tags that appear inside a div with class xyz
<div class="xyz">
  <b><a>Hello_1</a></b>
</div>
<div class="xyz">
   <b><a>Hello_2</a></b>
</div>

The following doesnt work?
$('.xyz').each(function() {
   var $this = $(this);

   $this.find('a').text().replace(/_/g, ' '));
});

A:

There is no problem with the regex. You are not updating the text in DOM.
.text() gives the textContent of an element, it doesn't set it. You can use text() setter as follow
$('.xyz a').text(function(i, text) {
    return text.replace(/_/g, ' ');
});

$('.xyz a').text(function(i, text) {
  return text.replace(/_/g, ' ');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="xyz">
  <b><a>Hello_1</a></b>
</div>
<div class="xyz">
  <b><a>Hello_2</a></b>
</div>

