0

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.

5
  • I would, if possible, recommend that you try to fix the problem in the data source rather than dealing with the issue in the UI Commented Jul 10, 2014 at 14:52
  • jsfiddle.net/8Zbcu/6 should do it. Commented Jul 10, 2014 at 14:54
  • A good way of handling this would be to have a value for displaying the currency and a hidden value containing just the number for further submiting and computations. If the only problem is display you can just remove "$ " but be aware that positioning of the currency depends on culture. Commented Jul 10, 2014 at 14:54
  • Thanks everyone!!! While each of these solutions worked, the winning snippet was provided by @NiettheDarkAbsol! jQuery('.currency strong').wrapInner('<span id ="dollarSymb"></span>').text(function (_, old) { return old.replace(/\s/g, ''); }); Those of you who take the time to help others learn are truly appreciated. Thanks again! Commented Jul 10, 2014 at 16:52
  • @user3582095 Guess I'd better post as an answer then! Commented Jul 10, 2014 at 16:53

6 Answers 6

2

You can add this code:

jQuery('.currency').find('strong').text(function(_,txt){
   return txt.replace(" ","");// or txt.replace(/\$\s/,'$');
});
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

You could also use (as mentioned in other answers) the whitespace operator \s instead of the space
0

Adding this should do it:

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

Demo

Comments

0

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

Comments

0

You can use regex with:

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

$element.text(newText);

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.