简体   繁体   English

RegExp 用于干净的文件和文件夹名称

[英]RegExp for clean file and folder names

I'm building a File manager with jQuery.我正在用 jQuery 构建一个文件管理器。 Users can create folders and upload files.用户可以创建文件夹和上传文件。 I need a Regual Expression (javascript) to check if the entered folder-name or the uploaded file-name is web-save.我需要一个正则表达式 (javascript) 来检查输入的文件夹名或上传的文件名是否为网络保存。 (no special chars or spaces are allowed) (不允许使用特殊字符或空格)

The name should only contain alphanumeric values (az,AZ,0-9), underscores (_) and dashes(-)名称应仅包含字母数字值 (az,AZ,0-9)、下划线 (_) 和破折号 (-)

Don't bother your visitor, do it for him :)不要打扰你的访客,为他做吧:)

var cleanName = function(name) {
    name = name.replace(/\s+/gi, '-'); // Replace white space with dash
    return name.replace(/[^a-zA-Z0-9\-]/gi, ''); // Strip any special charactere
};

cleanName('C\'est être');

This seems quite straightforward:这看起来很简单:

/^[\w.-]+$/

Useful tutorial有用的教程

function clean(filename) {
filename= filename.split(/[^a-zA-Z0-9\-\_\.]/gi).join('_');
return filename;
};

filename=clean("Vis'it M'ic ro_pole!.pdf");

This a regex to sanitize Windows files/folders.这是一个用于清理 Windows 文件/文件夹的正则表达式。 It will work for POSIX (linux, mac) too since it's less restrictive.它也适用于 POSIX(linux、mac),因为它的限制较少。

 function sanitizePath (path) { return path.replace(/[\\\\/:*?"<>|]/g, '') } console.log(sanitizePath('\\\\x/:a*b?<>|y'))

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

相关问题 使用regexp javascript从源文件中查找所有导入的文件名 - find all imported file names from a source file with regexp javascript 读取 HTML 页面中本地文件夹中的文件名 - Read file names in a local folder in an HTML page javascript正则表达式匹配标记名称 - javascript regexp match tag names 如何使用 Nodejs 将文件夹中的数据作为文件名写入 CSV 文件 - How to write a data as file names in a folder to CSV file using Nodejs 从指定文件夹和所有子文件夹获取文件名的循环 - js - For loop that gets file names from specified folder and all subfolders - js 根据最后一个前缀数字为文件夹中的文件名添加数字前缀 - Add number prefix to file names in a folder, based on the last prefix number 在服务器文件夹中获取文件名的工作解决方案 - Working solution to get file names inside server folder 如何访问特定文件夹以获取扩展名中的文件名 - How to access a specific folder to get file names within my extension 使用文件夹路径名,保存所有图像文件和文件名 - Using folder pathname, save all image files and file names 如何使用javascript打开文件夹并在其中列出html文件名? - how to use javascript to open a folder and list html file names in that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM