![](/img/trans.png)
[英](Javascript) How to extract the last part of the current URL - without the file extension?
[英]Extract part of current url to use in javascript function
希望对javascript有一点了解的人能够对我有所帮助。 我需要提取页面页面的一部分网址以在javascript函数中使用并附加到网址中。 (这是用于电源检查设置的。)我需要提取的部分是以下示例的编号,即。 www.mydomain.com/my-product-could-be-i950.html-因此只需要950部分即可。数字部分可以是2,3,4个字符。 然后,我需要将此附加到网址www.mydomain.com/write-a-review.html?pr_page_id=950
谁能帮忙,说实话,这超出了我一点。
非常感谢..内森
var num = location.pathname.match(/(\d+)\.html$/);
if( num ) {
var url = 'www.mydomain.com/write-a-review.html?pr_page_id=' + num[1];
}
尝试这个:
<script type="text/javascript">
function changeURL()
{
var szURL = location.pathname;
szURL = "www.mydomain.com/my-product-could-be-i950.html";
var num = szURL.replace(/\D/gi, "");
szURL = "www.mydomain.com/write-a-review.html?pr_page_id=" + num;
//Set URL
}
</script>
您可以使用正则表达式:
var re = /i([0-9]{2,4})\.html$/;
var m = document.location.match(re);
if (m){
document.location.href = 'http://www.mydomain.com/write-a-review.html?pr_page_id='+m[1];
}
//current page URL
cur = window.location
//regex to match your number
regex = /i[0-9]{2,4}\.html/;
//your number
num = cur.match(regex);
alert(num);
未经测试,请注意变量num可以是数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.