var str="http://localhost/2/2053/index.htm"; var r=/^http:\/\/localhost\/(\d+)\/(\d+)\/[\s\S]*/ alert(str.replace(r,'/file.php?url=$1/$2'));