Javascript 如何计算输入字段中基于数字的字符数?
我可以获得字符串中所有字符的计数,如Javascript 如何计算输入字段中基于数字的字符数?,javascript,html,validation,input,user-input,Javascript,Html,Validation,Input,User Input,我可以获得字符串中所有字符的计数,如e.target.value.length。但如何仅对字符串中基于数字的字符执行此操作?例如,对于像“123 abc”这样的字符串,如何编写一个表达式,将基于数字的字符计数输出为3?首先用空字符串替换所有非数字: str.replace(/\D+/g, '').length 您可以使用正则表达式计算(单个)匹配位数: const input=“123 abc”; 常数re=/\d{1}/g console.log(input.match(re.length
e.target.value.length
。但如何仅对字符串中基于数字的字符执行此操作?例如,对于像“123 abc”这样的字符串,如何编写一个表达式,将基于数字的字符计数输出为3
?首先用空字符串替换所有非数字:
str.replace(/\D+/g, '').length
您可以使用正则表达式计算(单个)匹配位数:
const input=“123 abc”;
常数re=/\d{1}/g
console.log(input.match(re.length))代码>