我使用以下命令获取当前位置的URL:
var currentLocation = window.location;
我总是会得到一个类似于:http://2016.test.ch/v3/index.html的值。第一个文件夹(本例中为v3)是网站原型的版本。如何访问url中的这个特定的第一个文件夹值并将其更改为类似于http://2016.test.ch/v2/index.html的内容
谢谢你的帮忙
发布于 2016-02-20 19:49:13
试试这个:
^(.*\/\/[^\/]+\/)(\w+)(.*)$
然后将其替换为:
$1v2$3 // {v2} could be anything else
var currentLocation = 'http://2016.test.ch/v3/index.html'; // window.location
var FirstFolder = 'v2'; // or any version you want
alert(currentLocation.replace(/^(.*\/\/[^\/]+\/)(\w+)(.*)$/, '$1'+FirstFolder+'$3'));
发布于 2016-02-20 19:49:18
找到数字并对其执行.replace
操作。就这么简单。
var currentLocation = window.location.toString(); // avoid changes to URL in the browser
var newLocation = currentLocation.replace(/\/v(\d+)/, function(whole, num){
return "/v" + (+num - 1);
});
当然,假设你自己也说过:
我总是会得到一个类似:http://2016.test.ch/v3/index.html的值。
在URL中有一个/v{digit(s)}
位
https://stackoverflow.com/questions/35522629
复制相似问题