我正在尝试使用正则表达式进行查找和替换,并将第一个(仅在此情况下)反向引用作为变量。
var RegexSearchString = "/((?:^(?:https?://|www\\d{0,3}[.])(?:.*?/)|(?:[a-z0-9.\\-]+[.][a-z]{2,4}/))(?:.*?/))/";
var test = "http://foo.bar.net/interestingbit/lots/of/other/stuff/here";
alert(test);
test.replace(umassURLRegexSearchString, "$1");
alert(test);
预期结果应该是一个提醒: http://foo.bar.net/interestingbit/lots/of/other/stuff/here
后跟一个提醒: http://foo.bar.net/interestingbit/
两个警报都包含首次初始化时测试变量的值。关于这个问题的任何想法?
答案 0 :(得分:0)
你需要:
包含所有这些更改的代码是:
var RegexSearchString = /^((?:(?:https?:\/\/|www\d{0,3}[.])(?:.*?\/)|(?:[a-z0-9.\-]+[.][a-z]{2,4}\/))(?:.*?\/))(?:.*?\/)*/;
var test = "http://foo.bar.net/interestingbit/lots/of/other/stuff/here/";
alert(test);
test = test.replace(RegexSearchString, "$1");
alert(test);
请在此处查看:http://jsfiddle.net/jheHR/1/