javascript替换字符串和url的字符之前

时间:2012-06-23 17:12:55

标签: javascript regex url

我有以下网址,我想找到一个字符串并删除该字符串及其前的任何内容。

字符串:me.do?page=

网址可以是

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

最终输出应为

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

3 个答案:

答案 0 :(得分:2)

即使对于这种情况,也不需要正则表达式:

var url = "http://demo.com/me.do?page=/facebook/link/home.php";
var result = url.split("/me.do?page=")[1];

答案 1 :(得分: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);

答案 2 :(得分:0)

如果你真的觉得你需要这个问题的正则表达式答案,并记住字符串操作更快/更便宜,那就是:

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