0

i have following urls, which i want to find a string and remove that string and anything before it.

String: me.do?page=

URL can be

http://demo.com/me.do?page=/facebook/link/home.php
http://demo.com/sub-folder/me.do?page=/facebook/link/home.php
http://**subdomain**.demo.com/sub-folder/demo/me.do?page=/facebook/link/home.php

final output should be

/facebook/link/home.php
/facebook/link/home.php
/facebook/link/home.php

3 Answers 3

2

Don't really need regex even for this case:

var url = "http://demo.com/me.do?page=/facebook/link/home.php";
var result = url.split("/me.do?page=")[1];
Sign up to request clarification or add additional context in comments.

2 Comments

note that if there is another "/me.do?page=" for some reason it will fail. To split and get everything after it even if the thing to split appears more than once this question will be useful
I should probably explain there's a few caveats to this code. One is that /me.do?page= has to exist in the string or split will return an array of 1 string and you will get an index out of bounds error from [1]. Second, like @ajax333221 said if there is more than 1 /me.do?page= it will (or should) only grab the result between the first and second instance. The other answers provided here are more robust in these manners, but this is a quick and dirty solution that's rather readable
1
 var ur="http://demo.com/sub-folder/me.do?page=/facebook/link/home.php";
  var answer=ur.substring(ur.indexOf("me.do?page=")+11, ur.length);

1 Comment

1st if anyone is wondering where the +11 came from its the length of me.do?page=. 2nd the second parameter for substring() can be omitted to grab the rest of the string which is what ur.length does. This is more robust than my answer as it will find the first instance of me.do?page= and return the rest, but we have a 'magic number' dependent on the length of the string your searching for...
0

If you really feel you need a regex answer for this question, and bear in mind that string manipulation is faster/cheaper, there's:

var urls = ["http://demo.com/me.do?page=/facebook/link/home.php",
                    "http://demo.com/sub-folder/me.do?page=/facebook/link/home.php",
                    "http://**subdomain**.demo.com/sub-folder/demo/me.do?page=/facebook/link/home.php"],
    newUrls = [];

for (var i = 0, len = urls.length; i < len; i++) {
    var url = urls[i];
    newUrls.push(url.replace(/.+\/me\.do\?page\=/, ''));
}

console.log(newUrls);​

JS Fiddle demo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.