reg exp to replace non-numeric characters?

Discussion in 'Javascript' started by laredotornado, Jul 13, 2007.

  1. Hi,

    I'm trying to replace all non-numeric characters with nothing in my
    variable. However, this doesn't seem to work ...

    var myMixedVar = myMixedVar.replace(/^[0-9]/, "");

    Any suggestions what I should have as the reg exp instead?

    Thanks, - Dave
     
    laredotornado, Jul 13, 2007
    #1
    1. Advertisements

  2. laredotornado

    d d Guest

    I'm no expert on regular expressions, but don't you want the ^ inside
    the square brackets, and then a g after the / to make it global.

    ..replace(/[^0-9]/g,"");

    ~dd
     
    d d, Jul 13, 2007
    #2
    1. Advertisements

  3. laredotornado

    Sudrien Guest



    ^[blah] : b, l, a, or h as the first character in the line
    [^blah] : can not match b, l, a, or h


    from http://www.javascriptkit.com/jsref/regexp.shtml:
    \D Match any non-digit. Equivalent to [^0-9].

    so it could also be written

    ..replace(/\D/g,"");

    -Sud.
     
    Sudrien, Jul 14, 2007
    #3
  4. In comp.lang.javascript message <
    oglegroups.com>, Fri, 13 Jul 2007 14:25:26, ""
    /\D/g

    See references via <URL:http://www.merlyn.demon.co.uk/js-valid.htm> or
    the link at the one instance of "regexp" in the FAQ.

    <FAQENTRY> To that FAQ link should be added one to a similar reference
    including newer features; or a new question should be added to introduce
    an answer including a set of RegExp references.

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
     
    Dr J R Stockton, Jul 14, 2007
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.