Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to find <h1>Kalani Doe</h1> but not <h1>Kalani Do | Profile</h1>

I need to find first names and last names inside H1 element such as,

<h1>Kalani Doe</h1>

But not one that has the word "Profile" in it, such as,

<h1>Kalani Doe | Profile</h1>

I know I need to use ?! but I can’t get around to understand how to combine positive and negative.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried this,

(<h1>(.+?)<\/h1>)(?!<h1>(.+?)Profile<\/h1>)

But it didn’t work.

>Solution :

you can use negative Lookbehind (?<!chars)

const str = `<h1>Kalani Doe</h1>
<h1>Kalani Doe | Profile</h1>
<h1>John  Doe</h1>`;

let m = str.match(/<h1>.*(?<!Profile)<\/h1>/gm)
console.log(m);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading