Basit
Basit

Reputation: 17214

javascript replace string and before chars of url

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

Upvotes: 0

Views: 381

Answers (3)

David Thomas
David Thomas

Reputation: 253486

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.

Upvotes: 0

Ashwin Singh
Ashwin Singh

Reputation: 7375

 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);

Upvotes: 1

Russ
Russ

Reputation: 1854

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];

Upvotes: 2

Related Questions