简体   繁体   English

字符串与正则表达式匹配?

[英]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) 标准化格式FW12_CommunicatedArticles.xlsx用户上传的任何文件应符合以下格式FW12_CommunicatedArticles.xlsx整数(FW中的1、2可以更改,但名称的其余部分应与指定的格式完全匹配)

for example a valid file upload can be FW13_CommunicatedArticles.xlsx , FW23_CommunicatedArticles.xlsx etc , etc 例如,有效的文件上传可以是FW13_CommunicatedArticles.xlsx,FW23_CommunicatedArticles.xlsx等,等等

invalid upload = sW13_CommunicatedArticles.xlsx , FW13_CommuArticles.xlsx , FW1324_CommuArticles.xlsx etc etc 无效上传= sW13_CommunicatedArticles.xlsx,FW13_CommuArticles.xlsx,FW1324_CommuArticles.xlsx等

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 只有整数的值可以不同,字符串的长度,其排列方式和所有内容都必须与约定中指定的完全相同,我必须通过javascript进行验证,请帮助我设计适当的正则表达式验证解决这个问题

This simple regexp will do the trick. 这个简单的正则表达式可以解决问题。 \\d will match a digit. \\d将匹配一个数字。

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

Here's a jsfiddle . 这是一个jsfiddle Press F1 and Esc in Chrome to show the console. 在Chrome中按F1和Esc键显示控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM