1

I have got this javascript on blank page:

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

so it shows

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

How do I extract only the numbers from an url so I would get this?

"Please show this number to your supplier: 123456789"
BenM
  • 52,573
  • 26
  • 113
  • 168
freewheeler
  • 1,306
  • 2
  • 12
  • 24

4 Answers4

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])
}
}   
0
document.URL.substring(document.URL.lastIndexOf('/'), document.URL.length);
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • While this might provide the solution in this particular case, it doesn't provide the answer to the question – Paarth Jul 18 '13 at 10:21
  • @Nadh: I don't think that's fair. There's nothing else that's shown here. It could easily be handled in another place. – Paarth Jul 18 '13 at 10:22
  • @Paarth I think he needs last part of the url, because url is like to be a service that gets invoices by id :) – karaxuna Jul 18 '13 at 10:26
  • Oh I agree with you and I do think you're being helpful in answering the spirit of the question, but I still think that it does not answer the question the poster asked. Specifically, how to get the numbers of out a URL. – Paarth Jul 18 '13 at 10:28
  • customer receives an email with simple hyperlink "DETAILS HERE" (href=http://www.exaple.com/invoices/123456789). The site has to show only "Please show this number to your supplier: 123456789" – freewheeler Jul 18 '13 at 10:30
  • @Paarth I agree with you, my answer does not really answer the title of the question. When somebody will search for solution they must not consider my answer, But it may be useful for current asker, so I will not delete it. Thanks – karaxuna Jul 18 '13 at 10:31
  • Thank u, but after a little bit more browsing, this absolutely does the trick, doesnt it http://forums.devshed.com/javascript-development-115/reading-url-passed-variables-in-javascript-41596.html – freewheeler Jul 18 '13 at 10:45
0
v = 'http://www.exaple.com/invoices/123456789';
number = v.replace(/[^0-9]/g,"");

console.log( number); //123456789
RRikesh
  • 14,112
  • 5
  • 49
  • 70
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);
}

$ = end of string.

RegExp.

Prasanth
  • 5,230
  • 2
  • 29
  • 61