Home > OS >  how can get the first div data with regex
how can get the first div data with regex

Time:12-29

I am trying to get the first div data through regex replace but i don't know how can i do that because first time i am doing this. I am trying to get the data from mail

JS

previousValueDisplay() {
   return this.previousValue
        .replace()
}

<div dir=\"ltr\">First div </div>
first div

i want to get the whole text from the first div like this

<div dir=\"ltr\">First div <div>is use </div><div>for test</div></div>
First div is use for test

CodePudding user response:

Maybe just parse your string as valid HTML input:

var parser = new DOMParser();
var htmlDoc = parser.parseFromString('<div dir=\"ltr\">First div <div>is use </div><div>for test</div></div>', 'text/html');
var htmlCollection = Array.from(htmlDoc.querySelector('div').innerText)
console.log(htmlCollection.join(""));

CodePudding user response:

here is a RegEx that will match your needs:

(!?(<.*?>)|[^<] )\s*

Thats how you get the Text between all tags in your email text. It even works for all other tags! (so its ignoring everything between < >)

Test Result

try it out

  • Related