Question

I need solution to find hashtag in url and append to last in that url using php.

Old URL : http://www.example.com?q=123123#anchor1?name=shreyas&city=surat

New URL : http://www.example.com?q=123123&name=shreyas&city=surat#anchor1

Was it helpful?

Solution

var url = 'http://www.example.com#anchor1?name=shreyas&city=surat';
var hash = /#[^?]+/.exec(url)[0];
url = url.replace(hash, '') + hash;

Jsfiddle

OTHER TIPS

You can also try this.

HTML :

<a href="http://www.example.com#anchor1?name=shreyas&city=surat">My Url</a>

jQuery :

var url= $("a").attr("href");
//get the subString matching that pattern "#...?"
var subUrl = url.match("\#[A-Z]*[a-z]*[0-9]*");
// removes the subString that matches the pattern "#...?"
url= url.replace(/\#[A-Z]*[a-z]*[0-9]*/, '');
//final url
url= url+ subUrl;
//updating it
$("a").attr("href", url);

jsFiddle Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top