![](/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.