I’m trying to build a regex to catch a useful part of my S3 filename uploads. I used a regex generator and so far I have this test (which results in an error thrown on javascript):
/[A-Za-z]++[^.w][^.]++|(?<=_)w++(?=.)/g
Here are some example strings that I am working with (with the require pattern to match):
"MTxoZbRRUu9BfQLvAWwP_Bruntwood Leeds Digital Festival ad.pdf" // desired match "Bruntwood Leeds Digital Festival ad"
"bbZRU3329BfXXvvAWwP_short-video.mp4" // desired match "short-video"
"zQZFnWVcRUbFNGyGdIP0_MGI-Artificial-Intelligence-Discussion-slides.pptx" // desired match "MGI-Artificial-Intelligence-Discussion-slides"
If it helps – I need to run this regex test on javascript.
const filename = "bbZRU3329BfXXvvAWwP_short-video.mp4";
const match = filename.match(regex);
console.log(match); // "short-video"
Thank you!
4
Answers
But not for JavaScript regexes, it seems. Every tool and library has its own regex quirks. In particular, JS doesn’t support possessive quantifiers like
++
(nor independent submatches in general,(?>
)
).JS also does not support look-behind,
(?<=
)
.You could e.g. do this instead:
Given your examples, you could use a much simpler regex:
Don’t use regex generators if they don’t provide your end regex flavor as flavors syntax and features may differ from each other. You are basically doing this:
with the only one difference that it matches preceding
_
character too that you can work around it later in JS.Live demo
For these example strings you could split on a dot and an underscore
[._]
That will give you an array with 3 parts. The values you are looking for are in the second part
[1]
: