The regular expression is used to process image addresses and img tags. The regular expression is img.

Source: Internet
Author: User

The regular expression is used to process image addresses and img tags. The regular expression is img.

You cannot upload an emoticon or image address directly.Therefore, you must process the img tag before uploading a comment or image. For example, we hope to help more programmers ~

<Span style = "font-size: 14px; "> // The first step is to obtain the string var str = 'image 1  Image 2  Image 3  '; </span>

Process the obtained string

<Span style = "font-size: 14px; "> // Replace the img tag with the specific encoding expression var reg =/] * src [= \" \ '\ s] + [^ \.] * \/([^ \.] + )\. [^ \ "\ '] + [\" \']? [^>] *>/Gi; str = str. replace (reg, "[face: faces 1.gif]"); console. log (str); </span>

Calculation Result:

<Span style = "font-size: 14px;"> image 1 [face: sanai.gif] Image 2 [face: em_23.gif] Image 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 html tags with image comments and upload information prohibited.

Next:

The running result returned from the previous step on the server is parsed in reverse mode. The method is as follows:

<Span style = "font-size: 14px;"> // Replace the specified encoding method with the expression var regg =/face :( [\ WMG +).gif/gi; str = str. replace (regg, "

The running result is:

<Span style = "font-size: 14px; "> image 1  Image 2  Image 3  </span>

The following is an html page of the Code, which can be copied and pasted to an html file and run directly. It is available for test.

<Span style = "font-size: 14px;"> <! DOCTYPE html> 

The above two methods can solve the positive and negative operations on the img tag. The first method is the previous collection, but you do not remember the original author. If there is any offense, you can propose a signature. The latter method is self-writing and has some limitations. You are welcome to discuss it ~

The above section describes how to use regular expressions to process image addresses and img tags. I hope this will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.