Strip all non-numeric characters from string in JavaScript

后端 未结 10 1598
醉酒成梦
醉酒成梦 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:21

    we are in 2017 now you can also use ES2016

    var a = 'abc123.8';
    console.log([...a].filter( e => isFinite(e)).join(''));
    

    or

    console.log([...'abc123.8'].filter( e => isFinite(e)).join(''));  
    

    The result is

    1238
    

提交回复
热议问题