Home > database >  Regex to match everything except a group
Regex to match everything except a group

Time:08-09

For a Javascript application I want to match everything in a text except some ids.

Id structure: <#(\w|-)#>

Input:

Lorem ipsum dolor sit amet, <#A-01#> consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et <#G-15#> dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation<#F-08#>ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure<#K-02#> dolor in reprehenderit <#S-21#> in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Expected match:

Lorem ipsum dolor sit amet, <#A-01#> consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et <#G-15#>dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation <#F-08#> ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure <#K-02#> dolor in reprehenderit <#S-21#> in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

It looks simple but I just couldn't find a solution :(

I hope it's clear, thanks for helping!

CodePudding user response:

First, your initial regex in the question is false (<#(\w|-)#>)


A match is a contiguous piece of string, you can't match AC in ABC in a single match.

However, you can isolate it in multiple matches:

enter image description here

/((?<=<#[A-Z]-[0-9]{2}#>)|^).*?((?=<#[A-Z]-[0-9]{2}#>)|$)/gm

That said if you want the string without the tags, you don't need everything I showed and can just remove it:

myString.replace(/<#[A-Z]-[0-9]{2}#>/gm, "")
  • Related