如何从变量中删除所有非数字字符

Har*_*rma 2 javascript

如何从 javascript 变量中删除所有文本字符(不是数字或浮点数)?

function deduct(){
    var getamt= document.getElementById('cf').value; //eg: "Amount is 1000"
    var value1 = 100;
    var getamt2 = (value1-getamt);
    document.getElementById('rf').value=getamt2;
}
Run Code Online (Sandbox Code Playgroud)

我想要getamt作为号码。parseInt正在给出NaN结果。

epa*_*llo 5

可以替换非数字

    var str = "Amount is 1000";
    var num = +str.replace(/[^0-9.]/g,"");
    console.log(num);
Run Code Online (Sandbox Code Playgroud)

或者你可以匹配号码

    var str = "Amount is 1000";
    var match = str.match(/([0-9.])+/,"");
    var num = match ? +match[0] : 0;
    console.log(num);
Run Code Online (Sandbox Code Playgroud)

比赛也可以更具体