2015-10-07 54 views
0

我有一個URL,如http://myurleg.com/ar/Message.html,我想用en代替ar,點擊它後。使用jquery從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> 

誰能幫助?

回答

1

您可以通過用戶的字符串replace

var str = "http://myurleg.com/ar/Message.html"; 
 
document.write(str); 
 
var res = str.replace("/ar/", "/fr/"); 
 
document.write('<br /> Result : ' + res);

1

split('/')陣列替換aren參加吧再次使用​​

var url ='http://myurleg.com/ar/Message.html'.split('/'); 
 
url[3]='en'; 
 
url=url.join('/'); 
 

 
document.write(url);

1
var current = location.pathname.substring(1, 3); 
var flipped = current == 'en' ? 'ar' : 'en'; 
location.pathname = '/' + flipped + location.pathname.substring(3); 
1

的一般解,在路徑的開始支持任意兩個字母的語言代碼,將是:

location.href = location.href.replace(/(\/\/.*?\/)\w{2}(.*)/, '$1en$2'); 

儘管有時它更有意義只操縱location.pathname

location.pathname = location.pathname.replace(/\/\w{2}(.*)/, '/en$1'); 
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");