2012-09-01 214 views
0

我想作一個URL重寫JavaScript中,我的問題是,如果我是這樣的:JavaScript網址重寫

var str='/test/service-34.htm', 
    exp='/test/service-[0-9]*.htm'; 
console.log(str.match(exp)); 

我會得到「/test/service-34.htm」作爲迴應,所以,如果我不能讓一個像更換:

/test/service-34.htm -> test.php?service=$1 

回答

4

嘗試:

"/test/service-34.htm".replace(
    /\/test\/service\-([0-9]+)\.htm/ 
, "test.php?service=$1" 
); 

創建backreference使用是很重要的您的更換中的[0-9]部件。您也可以使用+而不是*來設置所需的數字。

+0

失蹤*:P工作完美,ty。 系統不允許我標記這個anwser解決,4min能夠。 – chileodd