替换<li>中的文本

How can I replace - undefined with or remove it entirely? substr() is not a good idea because 'name' is always changing value. Please help.

<li>name - undefined</li>
<li>name - undefined</li>
<li>name - undefined</li>
<li>name - undefined</li>
<li>name - undefined</li>

here is my code...

 $.each(item.mguest,function(i,item)
 {
    $(".males-list li").first().after('<li class="mlist"><img src="icon/delete.png" title="REMOVE" class="delete-input" style="visibility:hidden;"/>' + item + '</li>')
});

You can use .text( function ) with replace() in a handler function to update the text of the element.

A function returning the text content to set. Receives the index position of the element in the set and the old text value as arguments.

Here is an example:

$(document).ready(function() {
  $('li').text(function(_, text) {
    return text.replace(' - undefined', ''); //In place of '' use text which you want.
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>name - undefined</li>
  <li>name - undefined</li>
  <li>name - undefined</li>
  <li>name - undefined</li>
  <li>name - undefined</li>
</ul>

</div>