我有以下类型的网址:localhost/Support/My-DNN-Module-Name/ID/133我需要始终在最后提取ID值,在本例中为133。这就是我目前正在做的提取它:if (url) { // try tograb number as int from url var intValue = url.match( /\d+/ ); if (intValue) { console.log("converted url to int value and the result is: " + intValue); } else { console.log("could not convert url to int value"); }}显然,如果URL包含其他数字,例如在本地主机作为实际IP地址输入的情况下,这不是很好。我怎么能只定位ID之后的最后一个部分,并抓住任何可能存在的数字,理想情况下,与之前的URL可能是什么样子无关?
1 回答

烙印99
TA贡献1829条经验 获得超13个赞
由于所需的数字始终以字符串结尾,因此可以将匹配的数字加到末尾:$
const str = 'localhost/12345/Support/My-DNN-Module-Name/ID/133';
const match = str.match(/\d+$/);
if (match) {
console.log(match[0]);
}
添加回答
举报
0/150
提交
取消