简体   繁体   中英

Regex to match everything except a group

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!

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:

https://regex101.com/r/khHY8F/1

在此处输入图像描述

/((?<=<#[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, "")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM