Jquery 替换多个URL中的子字符串

Jquery 替换多个URL中的子字符串,jquery,regex,Jquery,Regex,我的html: <select onchange="getval(this);"> <option value="user1">user1</option> <option value="user2">user2</option> <option value="user3">user3</option> </select> <a class="local" href="http

我的html:

<select onchange="getval(this);">
  <option value="user1">user1</option>
  <option value="user2">user2</option>
  <option value="user3">user3</option>
</select> 


<a class="local" href="http://subDomain.www.mysite.com">Site</a>
<a class="local" href="http://subDomain.www.mysite.com/admin">Admin</a>
<a class="local" href="javascript:open_wins('http://subDomain.mysite.com', 'http://subDomain.mysite.com/admin');">Both</a>

用户1
用户2
用户3
我的剧本:

<script>
  function getval(sel) {
    $(document).ready(function() {

      var x = (sel.value);
      var regex = new RegExp(Globally replace the subdomain of urls on select change);

      $('.local').attr('href', function(i, val) {
        return val.replace(regex, x);
      });
    });
  }
</script>

函数getval(sel){
$(文档).ready(函数(){
变量x=(选择值);
var regex=new RegExp(在选择更改时全局替换URL的子域);
$('.local').attr('href',函数(i,val){
返回值替换(regex,x);
});
});
}
我希望能够动态地将当前子域字符串(例如user1)替换为我从表单中选择的选项值

这个脚本可能并不优雅,但它可以替换url中的静态字符串。我试图做的是替换“http://”和“.”之间的所有内容,但尽管我尝试了,我还是无法找到合适的正则表达式来执行此操作

function getval(sel) {
    var x = (sel.value);
    var regex = new RegExp(/http:\/\/\w+\./g);
    $('.local').attr('href', function (i, val) {
        return val.replace(regex, "http://" + x + '.');
    });
}

这里有一个可用的提琴:

您不应该在
getval
函数中有一个
$(文档).ready
。如果有的话,它应该是包装的。你的意思是
http://subDomain.mysite.com
应该变成
http://user1.mysite.com
?mvChr:你是对的,我可以解决这个问题。当我从我的选择框中选择user2时,user1需要变成user2,等等。爆炸药丸:这几乎有效,但它去掉了最后一个“.”,例如subDomain.www.mysite.com变成了user1www.mysite.com。感谢大家的聪明、快速响应!hjpotter92的解决方案非常有效。