JavaScript regular gets the img tag alt value code

Source: Internet
Author: User

When converting between the discuz! visual editor and the Ubb editor, such as

The code is as follows Copy Code

But the content that IE gives is

The code is as follows Copy Code


Good double quotes just give me eat, are you hungry?
The original use is re =/alt= (["']+)" ([ss]*?) (1)/I; to match out the alt value.
Results IE gives the content can not match, the key is not double quotes, and alt value has a bit arbitrary value, Firefox does not have this problem. is very depressed, have the opportunity to let me see the person who developed IE browser kernel, I must throw a lump of excrement to him, estimate each and JavaScript and each version of the browser big dealings with the peers have the idea of it:

The Final Solution is: Add a plus a regular

The code is as follows Copy Code
Re =/alt= ([ss]*?) (>|s>|ssrc|sborder|swidth|sheight|s/>| ") /I;

To match the non-standard content generated by IE.

The detailed code is as follows:


The following ALT value is for the standard browser that is alt= "xxxx" with double quotation mark mode;

The code is as follows Copy Code

Re =/alt= (["']+) ([ss]*?) (1)/I;

var matches = re.exec (attributes);

if (matches!= null) {

var alt = matches[2];

if (alt.length) {

Return ' [img= ' + src + '] ' + ALT + ' [/img] ';

}

}

The following ALT value is for non-standard browsers such as Dog Day ie, alt=xxxx, which is not enclosed in double quotes;

The code is as follows Copy Code

Re =/alt= ([ss]*?) (>|s>|ssrc|sborder|swidth|sheight|s/>| ") /I;

var matches = re.exec (attributes);

if (matches!= null) {

var alt = matches[1];

if (alt.length) {

Return ' [img= ' + src + '] ' + ALT + ' [/img] ';

}

}

If you have any better way please be sure to tell me Oh!! ~~

PM Update:
Thanks to Blankyao's reminder, I found the attributes in front of the last is no > symbol
So the regular revision is changed to

  code is as follows copy code
re =/alt= ([SS] *?) (ssrc|sborder|swidth|sheight| ") /I;
Related Article

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.