Return only numbers from string

后端 未结 9 1998
名媛妹妹
名媛妹妹 2021-02-05 07:55

I have a value in Javascript as

var input = \"Rs. 6,67,000\"

How can I get only the numerical values ?

Result: 6

相关标签:
9条回答
  • 2021-02-05 08:51

    If by chance you need the comma you can use the code bellow:

    var input = "Rs. 6,67,000"
    const numbers = str.match(/(\d|,)+/g).pop();
    
    // 6,67,000
    
    0 讨论(0)
  • 2021-02-05 08:55

    For this task the easiest way to do it will be to us regex :)

    var input = "Rs. 6,67,000";
    var res = input.replace(/\D/g,'');
    console.log(res); // 667000

    Here you can find more information about how to use regex:

    https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

    I hope it helped :)

    Regards

    0 讨论(0)
  • 2021-02-05 08:55

    Try this

    var input = "Rs. 6,67,000";
    var res = input.replace(/Rs. |,/g, '');
    alert(res); // 667000

    JsFiddle

    Thanks,

    SuperCoder

    0 讨论(0)
提交回复
热议问题