如何使用js正则表达式验证文件夹名是否符合规范
背景:
git提交代码时需要验证新增的文件名称是否复合规范。具体规范如下:
全部采用小写方式, 以中划线分隔,用 kebab-case方式 (小写、短横线连接方式) 正例:
scripts / styles / components / images / utils / layouts / demo-styles / demo-scripts / img / doc
反例:script / style / demo_scripts / demoStyles / imgs / docs
【特殊】VUE 的项目中的 components 中的组件目录使用PascalCase格式(与驼峰法相似,单词大写开头)
因此需要验证普通文件名是否符合中划线命名法(kebab-case)以及vvue的component下的文件夹 大驼峰命名法(CamelCase)
中划线命名法(kebab-case):
思路:
1、排除文件名有空格
2、排除大写字母
3、连接方式为其他方式比如:—— _ . 等(反推:命名中只能有小写字母和-)
4、排除关键字:script style imgs docs等等
结论:
1、限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾:/^[a-z]{1}[a-z-]+[a-z]$/。
2、排除关键字。
// 关键字 const KEYWORDS = ["script","style","imgs","docs","break","else","new","var","case","finally","return","void","catch","for","switch","while","continue","function","this","with","default","if","throw","delete","in","try","do","instranceof","typeof","abstract","enum","int","short","boolean","export","interface","static","byte","extends","long","super","char","final","native","synchronized","class","float","package","throws","const","goto","private ","transient","debugger","implements","protected ","volatile","double","import","public"] // 正则:限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾 let reg = /^[a-z]{1}[a-z-]*?[a-z]$/; // /^[a-z]{1}[a-z-0-9]*?[a-z0-9]$/ (这个是限定以小写字母开头,中间小写or数字,结尾数字。两位数的情况下中间可以省略) // 要判断的文件名 let fileName = `xxxxx` if(KEYWORDS.indexOf(fileName) > -1){ // 文件名是关键字 }else if( reg.test(fileName) ){ // 判断:限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾 // 符合要求 }else{ // 不符合要求 }
大驼峰命名法(CamelCase):
// 正则:限定以大写字母开头,并且中间只包括大写or小写字母,以小写字母结尾 let reg = /^[A-Z]{1}[a-zA-Z]*?[a-z]$/; // 关键字 const KEYWORDS = ["script","style","imgs","docs","break","else","new","var","case","finally","return","void","catch","for","switch","while","continue","function","this","with","default","if","throw","delete","in","try","do","instranceof","typeof","abstract","enum","int","short","boolean","export","interface","static","byte","extends","long","super","char","final","native","synchronized","class","float","package","throws","const","goto","private ","transient","debugger","implements","protected ","volatile","double","import","public"] // 大写首字母字母的关键字 let up_keywords = KEYWORDS.map(item=>{ return item.charAt(0).toUpperCase() + item.slice(1); }) // 要判断的文件名 let fileName = `xxxxx` if(up_keywords.indexOf(fileName) > -1){ // 文件名是关键字 }else if( reg.test(fileName) ){ // 判断:限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾 // 符合要求 }else{ // 不符合要求 }
小驼峰命名法(lowerCamelCase):
// 正则:限定以大写字母开头,并且中间只包括大写or小写字母,以小写字母结尾 let reg = /^[a-z]{1}[a-zA-Z]*?[a-z]$/; // 关键字 const KEYWORDS = ["script","style","imgs","docs","break","else","new","var","case","finally","return","void","catch","for","switch","while","continue","function","this","with","default","if","throw","delete","in","try","do","instranceof","typeof","abstract","enum","int","short","boolean","export","interface","static","byte","extends","long","super","char","final","native","synchronized","class","float","package","throws","const","goto","private ","transient","debugger","implements","protected ","volatile","double","import","public"] // 要判断的文件名 let fileName = `xxxxx` if(KEYWORDS .indexOf(fileName) > -1){ // 文件名是关键字 }else if( reg.test(fileName) ){ // 判断:限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾 // 符合要求 }else{ // 不符合要求 }
补充:js正则匹配获取文件名
//获取文件名,不带后缀 var file_name=file_path.replace(/(.*\/)*([^.]+).*/ig,"$2"); //获取文件后缀 1.var FileExt=file_path.replace(/.+\./,""); 2.var fileExtension = file_path.substring(file_path.lastIndexOf('.') + 1); //截取文件后缀 var reg = /\.\w+$/; var file_name = file_path.replace(reg,'');
总结
到此这篇关于如何使用js正则表达式验证文件夹名是否符合规范的文章就介绍到这了,更多相关js正则验证文件夹名符合规范内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
BootStrap中的模态框(modal,弹出层)功能示例代码
bootstrap中的模态框(modal),不同于Tooltips,模态框以弹出对话框的形式出现,具有最小和最实用的功能集。这篇文章主要介绍了BootStrap中的模态框(modal,弹出层),需要的朋友可以参考下2018-11-11
最新评论