0

I have a URL say dummy URL

http://www.google.com/?v=as12&&src=test&img=test

Now I want to remove the &src=test& part alone.I know we can use indexof but somehow I could not get the idea of getting the next ampersand(&) and removing that part alone. Any help.The new URL should look like

http://www.google.com/?v=as12&img=test

6
  • var mystring = "google.com/?v=as12&&src=test&img=test"; mystring = mystring.replace(/\&src.*&\}/, ""); alert(mystring); Commented Oct 30, 2012 at 4:58
  • 1
    Did you try javascript replace() method, it may be helps Commented Oct 30, 2012 at 4:58
  • 1
    @Harish: That's right, post as answer... Commented Oct 30, 2012 at 4:59
  • that is not working unfortunately Commented Oct 30, 2012 at 5:01
  • 2
    @Harish: How is it not working? Can you post the test case? Commented Oct 30, 2012 at 5:01

3 Answers 3

3

What about using this?:

http://jsfiddle.net/RMaNd/8/

var mystring = "http://www.google.com/?v=as12&&src=test&img=test";
mystring = mystring.replace(/&src=.+&/, "");  // Didn't realize it isn't necessary to escape "&"
alert(mystring);

This assumes that "any" character will come after the "=" and before the next "&", but you can always change the . to a character set if you know what it could be - using [ ]

This also assumes that there will be at least 1 character after the "=" but before the "&" - because of the +. But you can change that to * if you think the string could be "src=&img=test"

UPDATE:

Using split might be the correct choice for this problem, but only if the position of src=whatever is still after "&&" but unknown...for example, if it were "&&img=test&src=test". So as long as the "&&" is always there to separate the static part from the part you want to update, you can use something like this:

http://jsfiddle.net/Y7LdG/

var mystring1 = "http://www.google.com/?v=as12&&src=test&img=test";
var mystring2 = "http://www.google.com/?v=as12&&img=test&src=test";

var final1 = removeSrcPair(mystring1);
alert(final1);
var final2 = removeSrcPair(mystring2);
alert(final2);

function replaceSrc(str) {
    return str.replace(/src=.*/g, "");
}

function removeSrcPair(orig) {
    var the_split = orig.split("&&");
    var split_second = the_split[1].split("&");
    for (var i = split_second.length-1; i >= 0; i--) {
        split_second[i] = replaceSrc(split_second[i]);
        if (split_second[i] === "") {
            split_second.splice(i, 1);
        }
    }
    var joined = split_second.join("&");
    return the_split[0] + "&" + joined;
}

This still assumes a few things - the main split is "&&"...the key is "src", then comes "=", then 0 or more characters...and of course, the key/value pairs are separated by "&". If your problem isn't this broad, then my first answer seems fine. If "src=test" won't always come first after "&&", you'd need to use a more "complex" Regex or this split method.

Sign up to request clarification or add additional context in comments.

4 Comments

@Harish Do you know for sure there will be a &key=value after the src=test?
@Harish Okay, I just wanted to make sure because if it doesn't have "&&" before it and "&" after it, you may have to use a modified regex or the split method to make it work. That's all I meant :)
@ianpgall - Great answer! Harish - If your requirements change with no last query string check out my answer
@Harish I just updated my answer with a solution using split. It seems like my original answer should work for you, but a more flexible url might require a more complex regex or using split like what I have provided.
1

Something like:

url = "http://www.google.com/?v=as12&&src=test&img=test"
firstPart = url.split('&&')[0];
lastPart = url.split('&&')[1];
lastPart = lastPart.split('&')[1];
newUrl = firstPart+'&'+lastPart;
document.write(newUrl);

Comments

1

Details: Use the split method.

Solution Edited: I changed the below to test that the last query string exists

var url = "http://www.google.com/?v=as12&&src=test&img=test";
var newUrl;

var splitString = url.split('&');

if (splitString.length > 3)
{
   newURL = splitString[0] + "&" + splitString[3];
}
else
{
   newURL = splitString[0];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.