如何使用js正则表达式验证文件夹名是否符合规范

 更新时间:2023年05月27日 09:28:56   作者:yaya小小狸  
众所周知正则表达式非常强大,下面这篇文章主要给大家介绍了关于如何使用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正则验证文件夹名符合规范内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JavaScript的防抖和节流案例

    JavaScript的防抖和节流案例

    这篇文章介绍了JavaScript的防抖和节流案例的代码实现和概念.包含详细的代码,希望对你有所帮助
    2021-06-06
  • JavaScript如何动态创建table表格

    JavaScript如何动态创建table表格

    这篇文章主要介绍了JavaScript如何动态创建table表格,一些时候需要动态的创建和删除表格,接下来的文章中将为大家介绍下javascript是如何做到的,感兴趣的朋友不要错过
    2015-11-11
  • JS canvas实现画板和签字板功能

    JS canvas实现画板和签字板功能

    这篇文章主要为大家详细介绍了JS canvas实现画板和签字板功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-02-02
  • BootStrap中的模态框(modal,弹出层)功能示例代码

    BootStrap中的模态框(modal,弹出层)功能示例代码

    bootstrap中的模态框(modal),不同于Tooltips,模态框以弹出对话框的形式出现,具有最小和最实用的功能集。这篇文章主要介绍了BootStrap中的模态框(modal,弹出层),需要的朋友可以参考下
    2018-11-11
  • ionic实现可滑动的tab选项卡切换效果

    ionic实现可滑动的tab选项卡切换效果

    这篇文章主要为大家详细介绍了ionic实现可滑动的tab选项卡切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • 纯javascript响应式树形菜单效果

    纯javascript响应式树形菜单效果

    这篇文章主要为大家分享了纯javascript响应式树形菜单效果的简单教程,对多级目录树形菜单感兴趣的小伙伴们可以参考一下
    2015-11-11
  • JS代码屏蔽F12,右键,粘贴,复制,剪切,选中,操作实例

    JS代码屏蔽F12,右键,粘贴,复制,剪切,选中,操作实例

    在本篇文章里小编给大家分享的是关于利用JS代码屏蔽F12,右键,粘贴,复制,剪切,选中,操作,需要的朋友们学习下。
    2019-09-09
  • JS删除数组指定值常用方法详解

    JS删除数组指定值常用方法详解

    这篇文章主要介绍了JS删除数组指定值常用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 让编辑器支持word复制黏贴、截屏的js代码

    让编辑器支持word复制黏贴、截屏的js代码

    这篇文章主要为大家详细介绍了让编辑器支持word复制黏贴、截屏的js代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • JS数组中对象去重操作示例

    JS数组中对象去重操作示例

    这篇文章主要介绍了JS数组中对象去重操作,涉及javascript数组对象遍历、比较、运算等相关操作技巧,需要的朋友可以参考下
    2019-06-06

最新评论