Matt Matt - 1 year ago 48
Javascript Question

Regex - Test extension if pass then test file name

Is it possible in

regex
to check file name if it pass the extension test,
i have a
regex
that matches
.local
OR
.txt
for the file like
something.local
OR
something.txt
. Is there anyway to strict one file name in the test ?

Right now i am using
/\.(?:local|txt)$/
but don't know how to implement that condition.
for example file name is test.raw.local so i want to check if it has raw.local then ignore that file .
I try
/\.(?:local|txt|!raw.local)$/
but didn't work!!

valid:


-> test.local, hello.txt, script.something.local

invalid:


-> test.raw.local

Answer Source

You can use a negative lookaheads in addition to your assertion for strings you want:

(?!.*\.raw\.local$)^.+\.(?:local|txt)$

Regex101 Example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download