javascript判断linux/windows合法路径
javascript判断linux/windows合法路径
·
function validateAbsolutePath(path,os) {
if (os ==="windows"){
let reg = new RegExp('^[a-zA-Z]:\\\\(((?![<>:"/\\\\|?*]).)+((?<![ .])\\\\)?)*$');
return reg.test(path)
}
if (os ==="linux"){
// reject not start with /
if (! new RegExp("^\/").test(path)){
return false
}
// approve root /
if (path === "/"){
return true
}
// reject double slash and \0
let reg2 =new RegExp("^(/)?([^/\0]+(/)?)+$")
return reg2.test(path)
}
}
var startList = [
'C://test',
'C://te?st.html',
'C:/test',
'C://test.html',
'C://test/hello.html',
'C:/test/hello.html',
'//test',
'/test',
'//test.html',
'//10.1.1.107',
'//10.1.1.107/test.html',
'//10.1.1.107/test/hello.html',
'//10.1.1.107/test/hello',
'//test/hello.txt',
'/test/html',
'/tes?t/html',
'/test.html',
'test.html',
'//',
'/',
'\\',
"C:\\\\hello\\\\world",
"C://hello//world",
'/t!esrtr',
'C:/hel**o',
'C:\\Users\\oneslide',
'c:\\',
'c:\\oneslide.html\\',
'C:\\hello\\新建文本文档.txt\\hello',
"C://hello world/",
"C://sta-world"
];
var linux_list=[
"/",
"/hello",
"/hello.html",
"/hello/hello/",
"/hello123#/hello",
"\\hello",
"/hello-world123",
"/hello\\",
"/hello//x/",
"/",
"/*hello.x",
"hello",
"/hello world/",
"/hello/张三.txt"
]
function testCase(reg,list){
let reg2 =new RegExp(reg)
list.forEach(item => {
console.log(reg2.test(item) + ' >>> ' + item);
});
}
startList.forEach(item => {
console.log(validateAbsolutePath(item,"windows") + ' >>> ' + item);
});
console.log("-----------------")
linux_list.forEach(item => {
console.log(validateAbsolutePath(item,"linux") + ' >>> ' + item);
});
更多推荐
所有评论(0)