user8189
user8189

Reputation: 79

string match with regular expression?

hey i have a requirement , where i have to compare a file name of the uploaded file to a standardized file name format specified by our client

Standardize Format FW12_CommunicatedArticles.xlsx any file that user uploads should be comply to the following format FW12_CommunicatedArticles.xlsx the integars (1, 2 in FW can change but the rest of the name should exactly match the format specified)

for example a valid file upload can be FW13_CommunicatedArticles.xlsx , FW23_CommunicatedArticles.xlsx etc , etc

invalid upload = sW13_CommunicatedArticles.xlsx , FW13_CommuArticles.xlsx , FW1324_CommuArticles.xlsx etc etc

only the value of the integars can be different , the strings length , its arrangement and everything has to be exactly the same as specified by the convention i have to do the validating through javascript , can you guys please help me devising the proper regular expression validation to tackle this issue

Upvotes: 0

Views: 143

Answers (1)

Dan Dascalescu
Dan Dascalescu

Reputation: 151926

This simple regexp will do the trick. \d will match a digit.

/^FW\d\d_CommunicatedArticles\.xlsx$/.test(filename)

Here's a jsfiddle. Press F1 and Esc in Chrome to show the console.

Upvotes: 3

Related Questions