validate created filename against a specified pattern in JavaScript - 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')

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$)/

Javascript RegEx named group and combine

I want to parse a file containing multiple lines. I will loop through the newlines to extract the information i need. I am trying to write a m3u parser which uses regex - i already succesfully made one using sting position and substring.
I want to parse this line:
#EXTINF:-1 tvg-ID="NPO 2 HD NL" tvg-name="NL : Npo 2" tvg-logo="http://www.iptv-plus.net:25461/images/Astra23picon/1_0_19_17C0_C82_3_EB0000_0_0_0.png" group-title="Holland",NL : Npo 2
So i came up with these regexpressions:
(?<=group-title=")(.*?)(?=")
(?<=tvg-name=")(.*?)(?=")
(?<=tvg-logo=")(.*?)(?=")
(?<=tvg-ID=")(.*?)(?=")
(?<=,)(.*?)$
These will extract the tvg tags, and extract the name from the end of the line (always after the last occuring comma.)
What i would like is to put all these regexpressions in one regex, so i can get an array containing all elements. I tried using | but that is an OR? i believe.. I can use them seperate, but i think it might be faster to put them all in 1? Also i would like to create named groups. But when i change it to
(?<group><=group-title=")(.*?)(?=")
it takes the lookbehind as text (<=).. Can i and how can i combine lookbehind with named groups? I like to use the regex in javascript, but could also implement a php that parses the m3u file and returns a json.

Concatenate Two Strings in HTML where one string has a Hyperlink in between

I have two strings in html. The two strings are "Forgotten your password?" and "Please" 'HPERLINK' "to change your password".
Now I have a messageBundle file which is a file to store constants. This file is used to translate the constants in other languages.
Now I have stored these strings in my constant file as:
Forgot_Pass="Forgotten your password?Please"
Forgot_password_continue="to change your password"
Then in html I am using this constants file to create a complete sentence on my UI as "Forgotten your password? Please click_here to change your password".
Here 'click_here' is the hyperlink.
Is there anyway I can store this error message in a single string in my MessageBundle file so that concatenation of these 2 strings can be avoided?
This seems to be the solution for me:
Forgot_Pass="Forgotten your password?Please Click here to change your password"
If I am wrong, please let me know.
You can do this by making use of a pre-defined pattern for variables (eg: ##1##, ##2## etc.,) that can be replaced with the corresponding values at the time of translation.
For example: You final (single) string could be as follows -
Forgot_Pass_Change="Forgotten your password? Please ##1## to change
your password"
where ##1## will need to be replaced with the <a href="http://your.link.path" >click_here</a> at the time of translation.
I understand that this means having to change the way the translation calls happen. Assuming that your current translation function is something like getTranslatedString(<translate_string_name>), you could override it to something like the one shown below:
getTranslatedString('Forgot_Pass_Change', [<variable_1>, <variable_2> ...]);
i.e
getTranslatedString('Forgot_Pass_Change', ['click_here_hyperlink']);
In your example you have only 1 variable string. However, there will be cases where you will need to replace more than 1 variable at a time (denoted by ##2##, ##3## and so on, in your template string). Hence the array implementation as the second parameter.

Capture value surounded by symbol in string

Inside my strings there is values surounded by $:
"$FORMAT$ is invalid, please check $FILE$ file"
I want to take variable names between $ (for example $FORMAT$ should be - FORMAT) to compare and if they match some condition replace variable in string with some value (for example ".txt"). I tried to achive that functionality with regex:
/\$(\w+)\$/gi
But it takes $ with variable names. How can I modify my regex to make it work properly? Thanks!
/\$(.+?)\$/gi should work well in your case.

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

Categories

Resources