Javascript 主题标签正则表达式提取 portlet 名称和页码

Javascript hashtag regex extract portlet name and page number

本文关键字:portlet 提取 标签 正则表达式 Javascript      更新时间:2023-09-26

你好在我的网站上,我在网址中使用哈希标签,比如#vehicle-list-page-25。我想做的是在一个字符串中提取vehicle-list-page,在另一个字符串中提取25。我怎样才能做到这一点?

我在这里测试正则表达式:

https://www.debuggex.com/

并设法创建了这个:

^[#][-A-Za-z]+[0-9]+$

它与主题标签匹配,但我不知道 hwo 是否可以删除我不需要使用正则表达式的部分,因为我没有使用它们的经验。我可以用.substr().search()来实现这一点,但我想有更好的方法?

您可以使用:

var m = '#vehicle-list-page-25'.match(/^#(.+?)-('d+)$/);

然后使用m[1]m[2]作为你的值。

console.log(m[1]); // vehicle-list-page
console.log(m[2]); // 25