Strip all non-numeric characters from string in JavaScript

后端 未结 10 1597
醉酒成梦
醉酒成梦 2020-11-22 13:44

Consider a non-DOM scenario where you\'d want to remove all non-numeric characters from a string using JavaScript/ECMAScript. Any characters that are in range 0 - 9

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 14:10

    Short function to remove all non-numeric characters but keep the decimal (and return the number):

    parseNum = str => +str.replace(/[^.\d]/g, '');
    let str = 'a1b2c.d3e';
    console.log(parseNum(str));

提交回复
热议问题