Js regular expression to match all images and image addresses in src

Source: Internet
Author: User

Js regular expression to match all images and image addresses in src

This article describes how to use JavaScript regular expressions to match all images and image URLs in src. Share it with you for your reference. The specific analysis is as follows:

There are many times when we need to use the image in the article, and mainly use its image address. In this case, we need to use regular expressions to match the Image Tag and then implement the data we need.

I didn't use regular expressions at ordinary times. I forgot it when I did not learn it. Recently, the project needed it, and then I went to goole again. It was so messy! I did not want to search for a large number of items. I finally saved myself a record and used it later:

Implementation: Use the js regular expression to match all images and all image addresses in src.

Idea: 1. Match the image img Tag (that is, match all images) and filter other unwanted characters

The image address (src attribute) is matched cyclically from the matching result (in the img tag)

Code: (you can copy it to a local device)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<Script type = "text/javascript">

// The idea is divided into two steps: Author (yanue ).

// 1. Match the image img Tag (that is, match all images) and filter other unwanted characters.

// 2. cyclically match the image address (src attribute) from the matched results (in the img tag)

Var str = "this is test string 123 and the end 33!

// Match the image (g indicates matching all results. I indicates case-sensitive)

Var imgReg =/|\/>)/Gi;

// Match the src attribute

Var srcReg =/src = [\ '\ "]? ([^ \ '\ "] *) [\' \"]? /I;

Var arr = str. match (imgReg );

Alert ('all arrays that have successfully matched images: '+ arr );

For (var I = 0; I <arr. length; I ++ ){

Var src = arr [I]. match (srcReg );

// Obtain the image address

If (src [1]) {

Alert ('matched image address' + (I + 1) + ':' + src [1]);

}

// Of course, you can also replace the src attribute

If (src [0]) {

Var t = src [0]. replace (/src/I, "href ");

// Alert (t );

}

}

</Script>

I hope this article will help you design javascript programs.

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.