如何只从URL获取数字?

时间:2013-07-18 10:18:07

标签: javascript html url

我在空白页面上有这个javascript:

Please show this number to your supplier:
<script>
document.write(document.URL);
</script>

所以它显示

"Please show this number to your supplier: http://www.exaple.com/invoices/123456789"

如何仅从网址中提取数字,以便获得此数据?

"Please show this number to your supplier: 123456789"

4 个答案:

答案 0 :(得分:1)

if (location.search != "")
{
var x = location.search.substr(1).replace(/%2520/g,' ').split(";")
for (var i=0; i<x.length; i++)
{
    var y = x[i].split("=");
    document.write(y[0])
}
}   

答案 1 :(得分:0)

document.URL.substring(document.URL.lastIndexOf('/'), document.URL.length);

答案 2 :(得分:0)

v = 'http://www.exaple.com/invoices/123456789';
number = v.replace(/[^0-9]/g,"");

console.log( number); //123456789

答案 3 :(得分:0)

var patt=/(\d+)$/;
var url=document.URL;
var num=0;
if (url.match(patt)) {
  var num_str=url.match(patt)[1];
  num=parseInt(num_str, 10);
}

$ =字符串结束。

RegExp