0

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

细绳: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
4

3 回答 3

2

即使在这种情况下也不需要正则表达式:

var url = "http://demo.com/me.do?page=/facebook/link/home.php";
var result = url.split("/me.do?page=")[1];
于 2012-06-23T17:19:16.363 回答
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);
于 2012-06-23T17:21:42.897 回答
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 小提琴演示

于 2012-06-23T17:23:06.283 回答