简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM