Extract number from string with RegExp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #1

    Extract number from string with RegExp

    I need an example that will extract a decimal number from a string using RegExp

    sample data would look something like this

    "US,GS 0.0 g↵"
    ",GS 164.3 g↵"


    and if no number is found I would want to return false
    "↵"
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    Not pretty, but at least this seems to work fine

    In the replace method below all characters not matching 0 to 9 or . are stripped from the msg and displayed in my output.
    Code:
    function dispWeight(msg) {
    	document.getElementById("curWeight").innerHTML = msg.replace( /[^.0123456789]/g,"");
    	}
    The RexExps is /[^.0123456789]/g
    The ^ says exclude these characters
    The [ ] says anything included here.
    The g say repeat for the entire string
    Last edited by Claus Mygind; Mar 31 '15, 06:29 PM. Reason: Clarification of the example

    Comment

    Working...