Regex to match all components that do not have a specific prop

Consider the following snippet:

<App>
  <Foo bar={1}>
    <p>Hello world!</p>
  </Foo>
  <Bar />
  <Bar foo={42} />
  <Bar baz={1} />
  <Bar a={1} b={2}>
    <p>Hello world!</p>
  </Bar>
  <Bar 
    a={1} 
    baz={2}
  >
    <p>Hello world!</p>
  </Bar>
</App>;

What is a regex that would match all Bar components that do not have a baz prop?

What I’ve tried

(?!.*?baz.*?[^<>\r]*)<Bar[^<>\r]*

This regex works when all props are on the same line (e.g. <Bar a={1} baz={2}>), but not when the props are on multiple lines.

<Bar
  a={1}
  baz={2}
>

Here’s a playground that shows the problem: https://regex101.com/r/0ZbDF6/1

Notice that the below is being matched, but it shouldn’t, because it has the baz prop.

  <Bar 
    a={1} 
    baz={2}
  >

Leave a Reply

Your email address will not be published.