Reputation: 25
My 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>
My script:
<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>
I want to be able to dynamically replace whatever the current subDomain string is, e.g., user1 with the option value I select from my form.
This script may not be elegant, but it works to replace a static string in the url. What I'm trying to do is replace everything between "http://" and ".", but try as I may, I cannot figure out the proper regex to do so.
Upvotes: 2
Views: 204
Reputation: 80639
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 + '.');
});
}
Here is a working fiddle: http://jsfiddle.net/3jbfD/
Upvotes: 2