JS extracts the numeric regular expressions in the string to remove non-numeric characters

JS extracts the number in a string

var str = "example12str933"
var res1 = str.replace(/\D/g, '') // 第一种替代所有非数字 \D
var res2 = str.replace(/[^\d]/g, '') // 第二种替代所有非数字 ^\d
var res3 = str.replace(/[^0-9]/g, '') // 第三种替代所有非 0-9 ^0-9
var resArry = str.match(/\d+/)[index] // 数组
// res1 12933
// res2 12933
// res3 12933
// resArry [12, 933]

Guess you like

Origin blog.csdn.net/qq_35714301/article/details/114920658