Regular Filter tag and retain the attributes inside the tag

A HTML in a text box contains text and img tags
for example

<img src="../../static/images/express/4.gif" alt="[em_4]"><img src="../../static/images/express/4.gif" alt="[em_4]">

make Filter look like this

[em_4][em_4]

I didn"t find it for a long time yesterday, but the rule is not very good. I want to ask for help. Thank you

.

see if this meets the requirements? Give me a compliment

var str = '<img src="../../static/images/express/4.gif" alt="[em_4]"><img src="../../static/images/express/4.gif" alt="[em_4]">';
var result = str.replace(/^.+alt="(.*)">(.*)<.*alt="(.*)".*$/, '$1$2$3');
console.log(result);

http://regex.zjmainstay.cn/r/.


regular expressions are mainly used to deal with strings. Since this is a DOM element, why do you use regular expressions?
you can use JQuery:

var str = $('img:eq(0)').attr('alt') + $('img:eq(0)'). text() + $('img:eq(1)').attr('alt')

_ em >

Update

the subject is given a string, so you can take the value by using the following regular formula:

reg1 = /\[.*?\]/g
reg2 = />.*?</g

var str = '<img src="../../static/images/express/4.gif" alt="[em_4]"><img src="../../static/images/express/4.gif" alt="[em_4]">'
var text = str.match(reg2)[0]
var result = str.match(reg1)[0] + text.substring(1, text.length - 1) + str.match(reg1)[1]
console.log(result)

Inspired by @ ymsd,

uses the following approach, which is not a regular solution, but also meets the requirements

 var str = '<img src="../../static/images/express/4.gif" alt="[em_4]"><img src="../../static/images/express/4.gif" alt="[em_4]"><img src="../../static/images/express/4.gif" alt="[em_4]">1111'

 var temp = str.replace(/<img.*?(?!alt=".*?")(>)/g, (match) => {
   return match.replace(/<img.*?(alt="(.*?)")(>)/g, '$2')
 })

result

[em_4][em_4][em_4]1111
Menu