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

Creating a regex to replace word based on condition

I have a file, with some data.
I need the regex that matches a text only within the Title=“”, but ignore if the text is within square brackets, and not others for ex. Link, BreakType etc

The word i want to match is Color.

<TocEntry
      Title="Color" - Highlight this text Color
Title=["Color"] - Ignore  this text Color
      Link="/Content/Color/" - ignore this text Color
      BreakType="Color" - ignore this text Color
      StartSection="false"
      PageNumberReset="Color"> - ignore this text Color
</TocEntry>
<TocEntry
      Title="Colord"> - ignore this text Colord
      //there could be Link="abc", BreakType="abc" here aswell
</TocEntry>
<TocEntry
      Title="dColord"> - ignore this text dColord
</TocEntry>

<TocEntry
      Title="my fav Color is red"> - Highlight this text Color
Title=["my fav Color is red"]> -  - ignore this text Color
</TocEntry>
<TocEntry
      Title="my fav 
Color 
is red"> - Highlight this text Color
</TocEntry>

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

>Solution :

If you’re looking to only match the word Color in those instances, this regex will work (run sample to view in action)
/(?<=Title="[^"]*)\bColor\b(?=[^"]*")/g

breakdown:
(?<=Title="[^"]*): positive lookbehind, ensure that the match is preceded by but does not include Title=" followed by 0 or more number of non-" characters

\bColor\b: match the word Color, but only if it has boundaries on both sides (not numbers or letters)

(?=[^"]*"): positive lookahead, ensure that the match is followed by but does not include any number of non-" characters followed by a single "

const input = `<TocEntry
      Title="Color" - Highlight this text Color
Title=["Color"] - Ignore  this text Color
      Link="/Content/Color/" - ignore this text Color
      BreakType="Color" - ignore this text Color
      StartSection="false"
      PageNumberReset="Color"> - ignore this text Color
</TocEntry>
<TocEntry
      Title="Colord"> - ignore this text Colord
      //there could be Link="abc", BreakType="abc" here aswell
</TocEntry>
<TocEntry
      Title="dColord"> - ignore this text dColord
</TocEntry>

<TocEntry
      Title="my fav Color is red"> - Highlight this text Color
Title=["my fav Color is red"]> -  - ignore this text Color
</TocEntry>
<TocEntry
      Title="my fav 
Color 
is red"> - Highlight this text Color
</TocEntry>`

const matches = input.match(/(?<=Title="[^"]*)\bColor\b(?=[^"]*")/g)

console.log(matches)

console.log(input.replace(/(?<=Title="[^"]*)\bColor\b(?=[^"]*")/g, '[MATCHED AND REPLACED]'))
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