[英]match multiple digits with str.replace()
My string is returning as empty when attempting to replace it with specified digits from the string.尝试用字符串中的指定数字替换它时,我的字符串返回为空。
const input = 4w12d3h;
let days = 0;
if (/d/.test(input)) {
days = input.replace(/.*(\d*)d.*/, '$1');
}
I expect the output to be '12' but it returns as ''.我希望输出为'12',但它返回为''。 If I instead do (\\d) or (\\d+) it will return as the digit before the letter (2), but I need to match as many digits as exist before the letter.如果我改为执行 (\\d) 或 (\\d+),它将作为字母 (2) 之前的数字返回,但我需要匹配字母之前存在的尽可能多的数字。
You can use match
also to get the desired output您也可以使用match
来获得所需的输出
const input = '4w12d3h'; let days = 0; if (/d/.test(input)) { days = input.match(/(\\d+)d/); } console.log(days[1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.