user5085217
user5085217

Reputation:

Jquery Remove White Space

Trying to remove unwanted whitespace & symbol from content.

using:

$('.infora .price .amount').text(function(index, text) {
  return text.replace('£', '');
  return text.replace(/ /g, '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<section class="infora">
  <p class="price"><span class="amount">
                          13.95                          </span>
  </p>
</section>

The pound sign get's removed fine but the white space still remain's.

Any idea's?

Upvotes: 0

Views: 83

Answers (2)

Tushar
Tushar

Reputation: 87233

Use \s+, this will remove all space characters. e.g. tabs. Using / /g will only remove space characters.

return text.replace('£', '').replace(/\s+/g, ''); // Thanks to @JaromandaX

Chain both replace statements. As you cannot return twice from a function. The second statement after return will not execute.

Edit

You can also combine the two replace into one regex.

return text.replace(/[£\s]/g, '')

Upvotes: 2

AmmarCSE
AmmarCSE

Reputation: 30607

You cannot return twice in a function. Also, for you case, trim() is useful

$('.infora .price .amount').text(function(index, text) {
  return text.replace('£', '').trim();
});

$('.infora .price .amount').text(function(index, text) {
  return text.replace('£', '').trim();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

<section class="infora">
  <p class="price"><span class="amount">
                          £13.95                          </span>
  </p>
</section>

Upvotes: 0

Related Questions