0

My code sample is as the following:

    let str = '<p>test xss<img src=x onerror=alert(1)><h1 onmouseover=prompt(0)></h1></p>';

    console.log(str.replace(/(<img [\s\S]*>?|<h1 [\s\S]*><\/h1>)/i, ''));

My purpose is very clear: extract img and h1 tag from str, so I expect the result is <p>test xss</p>, but actually the result is <p>test xss. It seems the regexp string img judgement part match the end of str. Can anyone tell me how to write this regexp string correctly ?

ahwyX100
  • 585
  • 2
  • 9
  • 22

3 Answers3

0

This is a little incomplete code, but just to give an example. Try something like this

let parser = new DOMParser()
let doc = parser.parseFromString(parser, "text/html")

and then you can operate on the doc like a HTMLDocument Node, with querySelectorAll and stuff

argentum47
  • 2,385
  • 1
  • 18
  • 20
0

How about this:

'<p>test xss<img src=x onerror=alert(1)><h1 onmouseover=prompt(0)></h1></p>'.replace(/<p>([\w ]+)\b.*<\/p>/, '<p>$1</p>')

?

Bob Dust
  • 2,370
  • 1
  • 17
  • 13
0

I change my regexp rule to /(<img [\s\S]*?>|<h1[\s\S]*\/h1>)/ig and it works now.

ahwyX100
  • 585
  • 2
  • 9
  • 22