Chris Cates
Chris Cates

Reputation: 121

Check whether text contains an image url (Regex)

So, let's say someone is typing something into a textarea.

Something as simple as this:

<textarea id="posttext" style="height: 50px; width: 80%;"></textarea>

I've already gotten the textarea to detect changes in the text area, however, let's say someone copy and pastes an image from another site, like this: http://celticevolution.com/images/test-201.gif

How would I get that image url? Also, let's say they were multiple images, and, how would I get it to display multiple images.

This is similar to how Facebook's posting system works.

Thanks, Chris.

Upvotes: 0

Views: 354

Answers (1)

raddrick
raddrick

Reputation: 4463

if you want to check to see if its an image url and react accordingly you can use a regular expression in your onchange event of the text area to see if it contains a gif, jpg, or png

var result = text.match(/http:\/\/\S+(\.png|\.jpg|\.gif)/g);

if you want to iterate through the pages images, you can capture the page in an ajax call, then do a different regex to match all the "results" (as matched above) and add them to an array, then iterate through them from the interface...like fb does.

Upvotes: 1

Related Questions