The code is:
var str = ‘<img src=”https://imgs.developpaper.com/imgs/logo.gif”>’;
var reg1 = /(\<img)(.*(?=(http|https)\:\/\/))((http|https)\:\/\/[^\/]*)/gim;
This usage is applicable when there is only one URL in the string, but if the string contains multiple domain names, for example:
After the program runs, the content removed is the second domain name https://www.jb51.net . Why?
A closer look at regular expressions shows that after matching < img with “(\ < IMG)”, use “. *” to match all characters until “http: / /” or “HTTPS: /”. Please note that it is “. *” that causes this problem. Here “. *” means to search and match as many as possible until the last qualifier, that is, greedy match in the term. Naturally, I thought of using non greedy matching to solve this problem. Change the expression to:
var reg1 = /(\<img)(.*?(?=(http|https)\:\/\/))((http|https)\:\/\/[^\/]*)/gim;
The method to solve the problem is very simple, but it also shows an important problem in daily work: inadequate program testing.