How does JQ intercept the src of img in the JS string and add a parameter

for example, a JS string like this

<img src="5.jpg">

AAP5

<b>1~2</b>

<img src="4.jpg">

1get

2

<b>2~4</b>

<p style="text-align: center; "><img src="3.jpg">

:

1;

2;

3TA;

4;

5;

6""

<b>5</b>

<p style="text-align: center; "><img src="2.jpg">

5:

1;

2;

39

<p style="text-align: center; "><img src="1.jpg">

TA

<br>

I want to extract the src, of all the img inside and add a parameter to it after the jpg, such as ? Xmurosq processing imagehand resizeMagime 500

.

is there any other way to do it if you don"t use rules?

Mar.29,2021

No regular but string traversal. By the way, it's convenient to use regularization, so why not allow it?


be ugly without regularization

var jsStr = '';

var size = "?x-oss-process=image/resize,h_500";
var result = jsStr;
while(result.indexOf('.jpg"') > -1 || result.indexOf('.png"') >-1) {
    result = result.replace('.jpg"', '.jpg' + size + '"').replace('.png"', '.png' + size + '"');
}
console.log(result);

think differently:

var size = "?x-oss-process=image/resize,h_500";
dom.html(str);
$.each('$(dom).find(img)',function(item,i){
    $(item).attr('src',$(item).attr('src')+size);
})
Menu