views:

38

answers:

4

I have some text I fetch inside here but only want the currency... How can I get rid of the other values?

I tried this, but didn't have any luck.

var symbol = $("div.price > h5 > div.num").text().replace(/[\d.]*/, "");

Here's the example html, the selector I'm using works fine, just not the replace.

<div class="price">
        <h5 class="biguns">
            <div class="num">
                €12.28
            </div>
            Lowest Price Per Night
        </h5>
    </div>
+2  A: 

The dot must be escaped othwerwise it will match every character and you must set the global modifier:

var symbol = $("div.price > h5 > div.num").text().replace(/[\d\.]+/g, "");
mck89
A: 

Test your regular expression before using it, I'm definitely not a regex expert which is why I always test it first :)

[\d|\.]+

John Strickler
Sorry but that one should be a comment instead.
Ruel
Hrmmm no because I want to provide a link and the answer too.
John Strickler
I see, well your initial `answer` doesn't have an answer.
Ruel
It did before you commented. Show me how to put links into comments and I'll take your suggestion.
John Strickler
@John Strickler - `[link text](url)` ;)
FreekOne
[link http://www.google.com/](http://www.google.com) Testing.. 1 2 3
John Strickler
NICE! :P thanks
John Strickler
You're welcome :)
FreekOne
A: 
var symbol = $("div.price > h5 > div.num").text().replace(/\d+\.?\d+/, "");
Ruel
Unfortunately this doesn't match 12 without any decimal places. Can't always assume you'll have a consistent input.
John Strickler
+1  A: 

If the currency is always the first character and it's one character long, you could easily get it with

var symbol = $(".num").text().substr(0,1);
FreekOne