Brion
Brion

Reputation: 83

Remove trailing whitespace via jQuery

I'm trying to eliminate the whitespace following a "$", which is actually part of a string (ex. $ 100.00), and is dynamically generated per user-input.

My knowledge of regular expressions is very limited. So I'm not sure if this is something I should be using. Or perhaps a more practical solution would be .trim(). But as I understand it, that method looks for beginning and ending whitespace.

Here's the markup I'm working with:

<div class="sideBy_side">
   Estimated Total Price ‡<br>
   <span class="currency"><strong>$ 172.84</strong> <a class="popup currency"
   href="/" title="USD">USD</a></span>
</div>

And my JSFiddle.

I'm still VERY new to jQuery and JS development in general. Any direction you can provide is appreciated.

Upvotes: 0

Views: 363

Answers (6)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Adding this should do it:

.text(function(_,old) {return old.replace(/\s/g,'');})

Demo

Upvotes: 0

Toby L Welch-Richards
Toby L Welch-Richards

Reputation: 817

You can do this in vanilla JS:

function stripWhitespaceAfterDollar(strInput){ var pattern = /\$( )+/; return strInput.replace(pattern,"$"); }

You want to look at pattern.

This is the regex here. To create a regex you put it within //.

We look for a dollar sign which is this part \$.
We need to escape the dollar because it is a regex operator.
Then we look for trailing whitepsace with ( )+.
(We don't need the parentheses but I put them there for clarity). We could use +
The + regex operator means one or more, so we look for one or more spaces.

Then we use the string builtin replace, which looks for a string or regex and replaces it.
In this case we are looking for a dollar and then whitespace, and replacing it with just the dollar sign.

Upvotes: 1

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

You could try something like this:

jQuery(function(​$) {
    var pane = $('#inputPane');
    pane.val($.trim(pane.val()).replace(/\s*[\r\n]+\s*/g, '\n')
                               .replace(/(<[^\/][^>]*>)\s*/g, '$1')
                               .replace(/\s*(<\/[^>]+>)/g, '$1'));
});

Which gives the result:

<p>some text here...</p>
<p>more text here...</p>

Upvotes: 0

Igor
Igor

Reputation: 33993

You can use regex with:

var $element = $('.currency strong');
var text = $element.text();
var newText = text.replace(/(\$)\s*/g, '$1');

$element.text(newText);

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59252

You can add this code:

jQuery('.currency').find('strong').text(function(_,txt){
   return txt.replace(" ","");// or txt.replace(/\$\s/,'$');
});

Upvotes: 2

ZiNNED
ZiNNED

Reputation: 2650

If the whitespace is always the second character, you don't need a regular expression. Something like this will do the trick:

//wrap total price in span tag
jQuery('.currency strong').wrapInner('<span id ="dollarSymb"></span>');

var price = $("#dollarSymb").html();
var cut = price.substring(0, 1) + price.substring(2);
$("#dollarSymb").html(cut);

FIDDLE

Upvotes: 0

Related Questions