[英]Using wildcard in string in Javascript
如何在 javascript 中表示字符串中的一系列数字?
例如,
var Ages = "该成员年龄 x 岁";
其中 x 可以是任何数字。
我希望能够在字符串数组中搜索这样的字符串。
Javascript 非常适合串联。
var x = // the age value you are searching for;
var ages = "The member is " + x + " years old";
然后,您可以将其包装在一个循环中。
使用正则表达式,匹配并捕获\d+
(一个或多个数字)代替x
:
const pattern = /This member is (\d+) years old/; const input = prompt('Input?', 'This member is 99 years old'); const match = pattern.exec(input); if (match) { console.log(match[1]); } else { console.log('Format not recognized'); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.