[英]remove a specific area from url using jquery
I have a URL such as http://myurleg.com/ar/Message.html
and I want to replace ar
with en
in it, after clicking on it. 我有一个网址,例如
http://myurleg.com/ar/Message.html
,在单击它之后,我想用en
替换ar
。
It means that if my url is: http://myurleg.com/ar/Message.html
这意味着如果我的网址是:
http://myurleg.com/ar/Message.html
: http://myurleg.com/ar/Message.html
After click it should become: http://myurleg.com/en/Message.html
单击后,它应变为:
http://myurleg.com/en/Message.html
: http://myurleg.com/en/Message.html
I just tried 我刚试过
<script>
$(document).ready(function () {
$('#lng_flip').click(function () {
var url = window.location.href.split('/')[0];
});
});
</script>
Can anyone help? 有人可以帮忙吗?
Replace ar
in split('/')
array with en
and join it again using join('/')
用
en
替换split('/')
数组中的ar
,然后使用join('/')
再次join('/')
var url ='http://myurleg.com/ar/Message.html'.split('/'); url[3]='en'; url=url.join('/'); document.write(url);
var current = location.pathname.substring(1, 3);
var flipped = current == 'en' ? 'ar' : 'en';
location.pathname = '/' + flipped + location.pathname.substring(3);
You can user string replace
: 您可以使用用户字符串
replace
:
var str = "http://myurleg.com/ar/Message.html"; document.write(str); var res = str.replace("/ar/", "/fr/"); document.write('<br /> Result : ' + res );
A general solution, supporting any two-letter language code at the beginning of the path, would be: 在该路径的开头支持任何两个字母的语言代码的通用解决方案是:
location.href = location.href.replace(/(\/\/.*?\/)\w{2}(.*)/, '$1en$2');
Though sometimes it makes more sense to only manipulate location.pathname
: 尽管有时只操作
location.pathname
更有意义:
location.pathname = location.pathname.replace(/\/\w{2}(.*)/, '/en$1');
Try this 尝试这个
var str = 'http://myurleg.com/ar/Message.html';
var txt = str.replace(/ar/i,"en");
Or in your case 还是你的情况
var url = window.location.href;
window.location.href = url.replace(/ar/i,"en");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.