Home > database >  Regex to select everything but a pattern of 13 digits in a row
Regex to select everything but a pattern of 13 digits in a row

Time:10-20

I have a String such as

https://www.mywebsite.com/123_05547898_8101060027367_00.jpeg

, and using Regex & NodeJs, I need to select everything except a pattern of 13 digits in a row (i.e. without other char types between digits).

Thus, I'm expecting to select:

https://www.mywebsite.com/123_05547898__00.jpeg

In other words, I would need the opposite of

\d{13}

Anyone got an idea?

Thanks for your help.

CodePudding user response:

You can use

text.replace(/\d{13}|(.)/g, '$1')
text.replace(/(_)\d{13}(?=_)|(.)/g, '$1$2') // only in between _s

See the regex demo.

The \d{13}|(.) pattern matches thirteen digits or any one char other than line break chars (LF and CR) while capturing it into Group 1. To put back this char, the $1 backreference is used in the replacement pattern.

Note there is no regex construct like "match some text other than a sequence of more than one character" (it is only supported in Lucene regex flavor that is rather a specific regex flavor). There is no way to emulate such a construct in JavaScript (it is possible in PCRE where you can use an alternation with (*SKIP)(*FAIL) and a tempered greedy token).

  • Related