tags:

views:

28

answers:

1

I have a bunch of textarea's on a page with copy and paste codes with urls inside them.

for exmaple:

<textarea><a href="http://anything.domain.com/x/5"&gt;something&lt;/a&gt;&lt;/textarea&gt;
<textarea><a href="http://anything.domain.com/x/3"&gt;something&lt;/a&gt;&lt;/textarea&gt;

I also have a dropdown, with a bunch of subdomain options with the intent of changing the subdomain inside all the textareas on the page.

e.g

<select class="changeLinks">
    <option value="www">www.domain.com</option>
    <option value="somethingelse">somethingelse.domain.com</option>
</select>

How to go through these textareas and just update the text of the subdomain?

$('.changeLinks').change(function(){

    var subdomain = $(this).val();

 // *blank* some sort of find and replace function which replaces whatever is after the http:// and before the first decimal with the var subdomain


});
A: 

You could use regular expression to find and replace all the links in the text:

var text = $("textarea").each(function(){
  $(this).text($(this).text().replace(/<a href="http:\/\/\w+\.domain\.com(.*?)">(.*?)<\/a>/gi, "<a href=\"http://"+subdomain+".domain.com"+$1+"\"&gt;"+$2+"&lt;/a&gt;"));
});

I haven't tested the function, but it should work. It might need some tweaking and bugfixing.

Marius
I don't quite understand how the regular expression works here. In the second part of the replace function you're cancelling out the quotes but not the first part. What does the /gi do? Sorry Marius I have to understand it before i can debug lol. thanks for this though it looks like it should work to me but I don't know why it isn't.
cosmicbdog
http://www.javascriptkit.com/javatutors/re.shtml
Marius