I want to check if the string contains html:
const containsHTML = (str) => /<[a-z][\s\S]*>/i.test(str);
console.log(containsHTML('<br>s')) // expect false
In my case i expect false
, because s
is not wrapped with HTML tag. How to fix the code?
I want to check if the string contains html:
const containsHTML = (str) => /<[a-z][\s\S]*>/i.test(str);
console.log(containsHTML('<br>s')) // expect false
In my case i expect false
, because s
is not wrapped with HTML tag. How to fix the code?
You could try this: /<([a-z]+.*)>.*<\/\1>/i
const containsHTML = (str) => /<([a-z]+.*)>.*<\/\1>/i.test(str)
console.log(containsHTML('<br>s')) // expect false
console.log(containsHTML('<p>s</p>')) // expect true
`? – trincot Nov 01 '21 at 17:42