SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to extract the number from this string

    Hi folks,
    I have a string, an example below, I want to extract the number from it.

    Code:
    <p class="the-price">&euro;15.00</p> 
    <p class="gbox"><!--<a href="#"><span>Arrange<br>
    Cover</span></a>--></p>
    Can anybody help with some reg exp for it?


    thanks in advance.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I usually "cheat" when doing this, meaning I do something like

    Code:
    <p>&euro; 15.00</p><p class="the-price" style="display:none">15</p>
    Simply because I feel that any regex is bound to fail some time (what if you don't take thousand separators into account, etc ...)

    Of course when you don't control the output of the website it becomes another story ...

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I would just do this:
    Code javascript:
    var frag = document.createElement('div');
    frag.innerHTML = theString;
    var price = frag.firstChild.firstChild.nodeValue.split(';')[1];
    With just regex:
    Code:
    var thePrice = /.+(\d+(\.\d+)?).+/.exec(theString)[1]


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •