[英]Using wildcard in string in Javascript
How can one represent a range of numbers in a string in javascript?如何在 javascript 中表示字符串中的一系列数字?
So for example,例如,
var Ages = "This member is x years old"; var Ages = "该成员年龄 x 岁";
where x can be any nunber at all.其中 x 可以是任何数字。
I want to be able to search for such string in an array of strings.我希望能够在字符串数组中搜索这样的字符串。
Javascript is really good for concatenations. Javascript 非常适合串联。
var x = // the age value you are searching for;
var ages = "The member is " + x + " years old";
You can then just wrap this in a loop.然后,您可以将其包装在一个循环中。
With a regular expression, match and capture \d+
(one or more digits) in place of the x
:使用正则表达式,匹配并捕获\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.