How to remove `&` sign and all text after that from url

  • A+
Category:Languages

I have this url https://myApp-ajj.com/sp?id=cat_item&sys_id=cf9f149cdbd25f00d080591e5e961920&sys_id1=cf9f149cdbd25f00d080591e5e961920&sysp_Id=a691acd9dbdf1bc0e9619fb&sysparm_CloneTable=sc_request&sysparm_CloneTable=sc_request

Here am getting sys_id two times with different parameters. So I need to remove the second & sign and all text after that.

I tried this

location.href.split('&')[2]  

I am sure it doesn't workout. Can anyone provide some better solution?

 


Firstly, you should split the string into an array then use slice to set the starting index number of the element which is 2 in your case and then join the array again into the string.

Read more about these methods JavaScript String split() Method, jQuery slice() Method and JavaScript Array join() Method

var url = 'https://myApp-ajj.com/sp?id=cat_item&sys_id=cf9f149cdbd25f00d080591e5e961920&sys_id=cf9f149cdbd25f00d080591e5e961920&sysp_Id=a691acd9dbdf1bc0e9619fb&sysparm_CloneTable=sc_request&sysparm_CloneTable=sc_request';  url = url.split("&").slice(0,2).join("&"); console.log(url);

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: