1

Hi again dear comminity!

To make it short, I have a textbox, where you can paste <img> tags, which look like this: <img src="123.jpg" />.

Is there any way to make these <img> look this: <img src="img/upload/username/123.jpg" /> using jQuery .replace() or something like this ? Or should I use PHP function instead ?

I have tried to find out what jQuery .match() returns for some regual expression:

x = text.match(/\<img src="(.*)" \/>\$/);
alert(x);

,but it returns odd things, when there are more than one <img> tag.

Thank you for your help!

2
  • Well, maybe I'm saying something wrong, but still is there any way to transform one src to another using javascript for all the specified "img" tags ? Commented Jul 4, 2011 at 13:44
  • you are looking for post 6572214 ;) Commented Jul 4, 2011 at 13:47

5 Answers 5

4

Not tested...

$('#text').change(function(){

    $('img').each(function(){
        $(this).attr('src', '/img/upload/username/' + $(this).attr('src'));
    });

});

If you would like to do this on server side with PHP you can just replace like this, no need for regex...

$text = str_replace('src="', 'src="/img/upload/username/', $text);

Or if you would like to use regexp...

$text = preg_replace('#"(.*?)\.jpg"#is', '"/img/upload/username/\\1.jpg"', $text);
Sign up to request clarification or add additional context in comments.

2 Comments

Damn, genious solution! Thank you so much!
@Dmitri, no problem, try replacing $('img') with eg $('#preview img'), otherwise it will replace all images on page ;)
1

The function you would like to use in jquery is .attr() and replace the content with whatever you like. Here is the tutorial page: http://api.jquery.com/attr/

1 Comment

Thank you too! Why didn't I thought of that in first place
1

but it returns odd things, when there are more than one tag.

I think it's the problem that you are not using a lazy match. Instead of (.*) use (.*?)

Comments

0
var text    =   '<img src="img/upload/username/123.jpg" />';
x = text.match(/\<img src="(.*)" \/\>$/);
alert(x);

now u ll get??

match(/\<img src="(.*)" \/\>$/);
                          ^
                          |-----------------Becarefull about about ur special char dude....

1 Comment

Doesn't seem to work well when there are more than 1 "img" tag!
0

Your regex returns not what you expect with more than one tag, because you use a greedy quantifier here:

x = text.match(/\<img src="(.*)" \/>\$/);
                             ^

To fix this try this:

x = text.match(/\<img src="(.*?)" \/>\$/);

I am not sure about your escaping ... . Normally you don't need to escape < and I also don't understand what the \$ is good for at the end. It is not in your string. If you want to have a anchor for string end then don't escape it, but if you want to be able to insert multiple tags, you can't use it at all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.