Link to home
Start Free TrialLog in
Avatar of Frank Danny
Frank DannyFlag for United States of America

asked on

How can I extract Id from a URL using Javascript?

Can anyone please  help? I  am trying to extract  an ID 254162  from  URL  http://localhost:9080/company_add.jsp?id=254162. I have  tried  to  write  as seen  below but I  when I  do  console.log,Java script  return /company_add.jsp only.Anything I am missing?.Thank you
     
var url=window.location.pathname;
            var  UrId=url.substring(url.lastIndexOf('=') + 1);

Open in new window

SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
var myurl = 'http://localhost:9080/company_add.jsp?id=254162';
console.log(myurl.replace(new RegExp(/.*id=(\d+)(\D.*)?/, ""), '$1'));

Open in new window

Output:
254162

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The regular expression works by looking at 3 parts of the URL:
1. the part up to and including id=
2. a number
3. everything after the number, if anything.

The replace command replaces the lot with just the value from 2. above. This is done by putting brackets around the part of the pattern used to match the number, which captures it as $1. It can then be used as the replacement for the whole string.
Another alternative not using Regex - gets the parameters into an Object
<script>
var inp = ' http://localhost:9080/company_add.jsp?id=254162&a=fred&b=donky&c=adsf09afua0ufa';

function getUrlParams(url) 
{
  var temp = url.split('?');
  var result = {}
  if (temp[1]) {  
    var params = temp[1].split('&');
    for(var i = 0; i < params.length; i++) {
      var t = params[i].split('=');
      result[t[0]] = t[1]
    }
  }
  return result;
}

var params = getUrlParams(inp);
if (params.id) console.log(params.id);
</script>

Open in new window

Avatar of Frank Danny

ASKER

Thank  you@Julian,  @Terry and @Hielo.I  did  decide  to  go with  Terry solutions but facing issue  with  regex  expression.I have tried to  test  regex expression on  regex101.com, with no  issue.Am I missing anything here(i.e I did  try to type on keyboard as well instead of copy-paste)User generated image?below is  the error under F12 tools on  internet  explorer   v11
One thing: I see
 var url=window.location.pathname

Open in new window

is returned as "/company_view.jsp"  after I  did  console.log.I want to  return a complete current url which is http://localhost:9080/company_add.jsp?id=254162
Now I  was able to  return the  URL by
var url=window.location.href

Open in new window

; but the  regex  failed  with the same error
The solution you are using does a replace - I thought you wanted to do an extract which would require a match

<script>
var url = window.location.href;
var result = url.match(/id=(\d+)/);
var id = result ? result[1] : false;
console.log(result);
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank  you@Julian...it works :)
You are welcome.