Javascript 使用jquery从url中删除特定区域
我有一个URL,比如Javascript 使用jquery从url中删除特定区域,javascript,jquery,html,Javascript,Jquery,Html,我有一个URL,比如http://myurleg.com/ar/Message.html单击后,我想将其中的ar替换为en 这意味着如果我的url是:http://myurleg.com/ar/Message.html 点击后应变成:http://myurleg.com/en/Message.html 我刚试过 <script> $(document).ready(function () { $('#lng_flip').click(function ()
http://myurleg.com/ar/Message.html
单击后,我想将其中的ar
替换为en
这意味着如果我的url是:http://myurleg.com/ar/Message.html
点击后应变成:http://myurleg.com/en/Message.html
我刚试过
<script>
$(document).ready(function () {
$('#lng_flip').click(function () {
var url = window.location.href.split('/')[0];
});
});
</script>
$(文档).ready(函数(){
$('#lng_flip')。单击(函数(){
var url=window.location.href.split('/')[0];
});
});
有人能帮忙吗?用
en
替换数组中的ar
,然后使用再次加入它
var url='1〕http://myurleg.com/ar/Message.html.split('/');
网址[3]=“en”;
url=url.join('/');
document.write(url)
用en
替换数组中的ar
,然后使用再次加入它
var url='1〕http://myurleg.com/ar/Message.html.split('/');
网址[3]=“en”;
url=url.join('/');
document.write(url)代码>您可以使用字符串替换:
var str=”http://myurleg.com/ar/Message.html";
文件编写(str);
var res=str.replace(“/ar/”,“/fr/”);
编写(“
结果:”+res)代码>您可以使用字符串替换:
var str=”http://myurleg.com/ar/Message.html";
文件编写(str);
var res=str.replace(“/ar/”,“/fr/”);
编写(“
结果:”+res)代码>支持路径开头任意两个字母的语言代码的通用解决方案是:
location.href = location.href.replace(/(\/\/.*?\/)\w{2}(.*)/, '$1en$2');
虽然有时只操作位置.路径名
更有意义:
location.pathname = location.pathname.replace(/\/\w{2}(.*)/, '/en$1');
在路径开头支持任何两个字母的语言代码的通用解决方案是:
location.href = location.href.replace(/(\/\/.*?\/)\w{2}(.*)/, '$1en$2');
虽然有时只操作位置.路径名
更有意义:
location.pathname = location.pathname.replace(/\/\w{2}(.*)/, '/en$1');
试试这个
var str = 'http://myurleg.com/ar/Message.html';
var txt = str.replace(/ar/i,"en");
还是你的情况
var url = window.location.href;
window.location.href = url.replace(/ar/i,"en");
试试这个
var str = 'http://myurleg.com/ar/Message.html';
var txt = str.replace(/ar/i,"en");
还是你的情况
var url = window.location.href;
window.location.href = url.replace(/ar/i,"en");