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 ~