Regex - Test extension if pass then test file name - javascript

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

You can use a negative lookaheads in addition to your assertion for strings you want:
(?!.*\.raw\.local$)^.+\.(?:local|txt)$
Regex101 Example

Related

How to create decorator to validate image URL in typescript

I have to validate an image by its URI or its type. If i use this regex pattern it validates only the link (http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png) ends with this types but in my case it should validate both like. It should accept URL or else file type.
Eg:
1) https://thevarsity.ca/wp-content/uploads/2019/09/ARTS-TIFF_REVIEW-COURTESY_PHOTOKINO_LORBER-IMAGE_BOOK_STILL-1080x720.jpg
2)
Should accept image types like PNG,JPG,JPEG,GIF.
sdfdsadasdsadsfdsf.png
And am looking of the solution.
You can check against multiple regex statements by separating them with | like regex_one|regex_two
To check for the end portion say the extension, you can use $ symbol
Hope the below mentioned regex works for you.
/(http(s?):)([\/|.|\w|\s|-])*\.(?:jpg$|gif$|png$)|([\/|.|\w|\s|-])*\.(?:jpg$|gif$|png$)/

validate created filename against a specified pattern in JavaScript

I am trying to find out if my created files have file names that are matching the pattern.
For example: My created file's file name will be in format 17-06-27_11-02-34_9530114
where the First part 17-06-27 : YY-MM-DD second part
11-02-34:Hour:Minutes:Seconds Third part 9530114: some id
how can I write a regular expression in JavaScript to check the pattern as well as check if the first two parts that is the date and time are matching the date and time of the birth of the file.
[0-9][1-9]-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])(0[0-9]|1[0-2])-([0-5][0-9])-([0-5][0-9])\d{7}
For testing purposes use this link
This regex will match the example you gave: /^((\d\d-){2}\d\d_){2}\d{7}$/
To test it in your browser's console, try this:
/^((\d\d-){2}\d\d_){2}\d{7}$/.test('17-06-27_11-02-34_9530114')

regex match extension but exclude a specific file

I am in webpack.config where I need to include extension to match specific file type, but I need to exclude a specific file. I googled it but didn't find a perfect solution.
What I have : /\.(?:css|less)$/ -> matches all files that have .less OR .css but here I want to add "not only -> test.something_strict.less" /\.(?:css|less|!test.less)$/ where test.something_strict is a file name with extension .less and only exclude test.something_strict
but this didn't work, this matches .less or .css and pass through the test.
|!test.less doesn't really negate matching test.less. It will literally match ! before test.less.
You can use this regex:
/^(?!test\.[^.]+\.less$).+\.(?:css|less)$/m
RegEx Demo
(?!test\.less$) is a negative lookahead that will assert failure if filename is test.<anything>.less.

Search and replace moduleFor('foo:bar') with moduleFor('foo:bar', 'unit#foo:bar')

I am working with a large set of qunit unit tests in a folder called "tests". At the top of each file is the following, for example:
moduleFor('mixin:foo')
// or
moduleFor('route:bar')
I'd like to write a search and replace for these files such that each occurrence would be replaced with this updated style:
moduleFor('mixin:foo', 'unit#mixin:foo')
// or
moduleFor('route:bar', 'unit#route:foo')
If the first line of your file is this line, you can use this, with multiline flag on.
\AmoduleFor\('(.*?)'\)$
Where \A always matches the beginning of the input data (file in this case). $ because of the multiline flag, matches the end of the line (not the input). You can also probably remove the $ in case of following whitespace or comments you don't want to affect. If this is always the total first line, I'd leave the $.
In this example here, I deliberately use ^ to match the beginning of the line rather than the input, merely for demonstration purposes.
I'm not familiar with qunit and if there might be no other occurences of module in the file, if there is not, assertions shouldn't be necessary.

Javascript - regex to check if user write correct formated input

in my CLI users can specify what they want to use:
A user command can look like this:
include=name1,name2,name3
category=name1,name2
category=name1
In another words, a command always consists of 3 parts:
command name: can be just include or category
=: is in every command
name or names of things they want to use, split by ,
How can I test this to get always true but false on everything else.
I am really bad in regex but I tried something like this:
/\category|include=\w/.test(str);
to simply test, at least, the most easy alternative which would be category=name1 but without success.
Can someone help me with this?
You were on the right path. Here's a fixed regex:
/^(category|include)=\w+(,\w+)*$/.test(str);
Note:
the parens around the alternative parts
the + after the \w so that you can have several characters
the optional (,\w+)*
the start and end of string marks (^ and $) in order to check the whole string
You can use this regex for your requorement:
/^(category|include)=(\w+(?:,\w+)*)$/
RegEx Demo
\w+(?:,\w+)*) in the value part after = will allow 1 or more of comma separated words.

Categories

Resources