在 Javascript 中的字符串中使用通配符

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');
}