Except for the img tag src attribute, all tag attributes are removed, how to write, can not write, very difficult oh

pastedText = "<a href=""></a><img src="love.png" /><iframe src="index.html"></iframe>";

all tag attributes except the img tag src attribute are removed. How to write

pastedText = pastedText.replace

May.18,2022

A rule should not be able to tell the difference between the two, because you need to exclude all tags, including the img attribute, while preserving the img src attribute

.
str.replace(/<img[^>]+(src=".+?")[^>]+>/g,'<img $1/>').replace(/(<(?!img)([A-Za-z]+)([^>]+)\>)/g,'<$2>');

str.replace(/<img[^>]+(src=".+?")[^>]+>/g,'<img $1/>').replace(/<(?!img)([A-Za-z]+)(?:[^>]+)\>/g,'<$1>');

pastedText = pastedText.match (/ ?)". ? > /) [0]


     let pastedText = '<a href=""></a><img src="love.png" /><iframe src="index.html"></iframe>';

     pastedText = pastedText.replace(/(.*)(<img.*? src=".*".*? \/>)(.*)/,function($1,$2,$3){
            return $3
        })
     console.log(pastedText)

1 if it is text, replace the tag attribute
2 in the text with regular.If it is already in dom, get the innerHTML of dom, and the problem turns to problem 1
problem 1 solution (read the thinking of the boss upstairs, if you understand something, add some boundary conditions to the code)

function formateHtml(str){
  return str.replace(/<(img)[^>]+(src=["|'].+?["|'])[^>]+>/ig,'<$1 $2/>').replace(/<(?!img)([\w|-]+)(?:[^>]+?)(\/)?\>/ig,'<$1$2>');
}

//
var str = `<a> </a><img src="imgSrc" draggable /><img src="src1" width="50" /><IMG SRC="SRC2" /><el-input v-model="val"></el-input><bb src="bbsrc" disabled >bb</bb>`;
console.log(formateHtml(str)); 
//<a> </a><img src="imgSrc"/><img src="src1"/><IMG SRC="SRC2"/><el-input></el-input><bb>bb</bb>

  

I don't understand what your problem is

Menu