How do I remove letters and dashes and dollar signs in a string using a regular expression?

前端 未结 6 1644
北海茫月
北海茫月 2020-12-23 21:33

I\'m trying to find all letters and dashes and dollar signs and remove them from a text box.

function numbersOnly()
{
    if ($(\'.sumit\').val().indexOf([A         


        
6条回答
  •  旧时难觅i
    2020-12-23 22:09

    I really like Mark's answer. However, depending on your programming language (and RegEx engine) \d matches characters like ४, or ৮, which are UTF-8 digits.

    So if you only want digits from 0 to 9, use [^0-9.]:

    $('.sumit').val().replace(/[^0-9.]/g, "");
    

    This is even faster if your RegEx engine is UTF-8 aware. An example Language, where this applies is python3 (http://docs.python.org/3/library/re.html - search for "\d"). The ECMA Standard, however, says that for JavaScript \d equals [0-9].

提交回复
热议问题