Javascript Regular Expressions - Replace non-numeric characters

前端 未结 10 959
清歌不尽
清歌不尽 2021-01-30 12:32

This works:

var.replace(/[^0-9]+/g, \'\');  

That simple snippet will replace anything that is not a number with nothing.

But decimals

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 13:11

    If you don't want to catch IP address along with decimals:

    var.replace(/[^0-9]+\\.?[0-9]*/g, '');
    

    Which will only catch numerals with one or zero periods

提交回复
热议问题