-2

I am trying to search some text which can search with or without html tag. Have tried with (?!([^<]+)?>) which is working with content which is outside of html tag.

Regex - (simple html)(?!([^<]+)?>)

Html - This is simple html text <span class='simple'>simple html</span> text simple <p>html</p>

but the content in simple <p>html</p> is not working.

Please note i am avoiding class='simple'.

santanu
  • 61
  • 11

1 Answers1

0

Using: /<\/?.*?>/g and replace, you can strip out all HTML tags, and simply search through the remaining text.

let regex = /<\/?.*?>/g,
  html = `This is simple html text <span class='simple'>simple html</span> text simple <p>html</p>`,
  string = html.replace(regex, "");

console.log(`Stripped HTML: ${string}`)

let find = "simple html",
  found = string.search(find);

console.log(`\n$ String "${find}" found at char ${found}`)
KyleFairns
  • 2,947
  • 1
  • 15
  • 35