Regular expression processing picture address, IMG Tag Method Summary sharing

Source: Internet
Author: User
This article mainly introduces the regular expression processing image address, img tag method, very good, with reference value, the need for friends can refer to the next

Uploading emoticons or image addresses can be a lot of times when you can't upload them directly , so you should deal with the IMG tag before uploading your comments or images. For example, hope can help more programs ape ~


<span style= "FONT-SIZE:14PX;" >//The first step is to get the string with the img tag var str = ' picture 1 Picture 2 Picture 3  ';</span>

Processing the obtained string


<span style= "FONT-SIZE:14PX;" >//Replace the IMG tag with a specific encoding for the expression var reg =/]*src[=\ "\ ' \s]+[^\. *\/([^\.] +)\. [^\"\']+[\"\']? [^>]*>/gi; str = str.replace (Reg, "[Face:$1.gif]"); Console.log (str);</span>

The result of the operation is:


<span style= "FONT-SIZE:14PX;" > Image 1[face:sanai.gif] picture 2[face:em_23.gif] Picture 3[face:451.gif]</span>

In this way, the code uploaded to the server will not have HTML tags, which is a good way to deal with image comments and upload information to prohibit HTML tags.

Next:

The result of the run from the previous step returned from the server is parsed in reverse, with the following method:


<span style= "FONT-SIZE:14PX;" >//An expression that replaces a specific encoding with an IMG tag var regg =/face: ([\w]+). Gif/gi; str = str.replace (Regg, "

The result of the operation is:


<span style= "FONT-SIZE:14PX;" > Pictures 1 Pictures 2 Pictures 3</span>

The following is attached to the code of the HTML page, you can copy and paste all directly into the HTML file to run, pro-Test available


<span style= "FONT-SIZE:14PX;" ><! DOCTYPE html> 

The above two methods are good to solve the positive and negative operation of the IMG tag, the first method for the previous collection, do not remember the original author, if there is an offence can be presented signature. The latter method for my self-writing, there are certain limitations, welcome to discuss ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.