使用js或jquery查找和替换

时间:2015-01-03 04:11:49

标签: javascript jquery

我的网址是在两种语言之间切换内容

http://mydomanin.org/km/components#children-with-disablities

http://mydomanin.org/en/components#children-with-disablities

var current_url = document.URL;
var match_url = current_url.replace(/\/km/g,'/en');
console.log(match_url);

我想如果找到\km将被\en替换,如果找到\en替换为\km

我该怎么做?

1 个答案:

答案 0 :(得分:3)

试试这个:

var replace = { km:"en", en:"km" };
str = str.replace(/km|en/gi, function(match){
  return replace[match];
});

<强> Demo