簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM