简体   繁体   English

正则表达式删除文件名中的特殊字符(扩展名除外)

[英]Regex remove special characters in filename except extension

I need to remove any special character from a filename except the extension. 我需要从扩展名中删除文件名中的任何特殊字符。

Im using the javascript filename.replace(regex, '-'); 我正在使用javascript filename.replace(regex, '-');

Original filename: manuel fernandex – Index Prot.bla.otype 5 (pepito grillo).jpg 原始文件名: manuel fernandex – Index Prot.bla.otype 5 (pepito grillo).jpg

Target filename: manuel-fernandex-Index-Prot-bla-otype-5-pepito-grillo-.jpg 目标文件名: manuel-fernandex-Index-Prot-bla-otype-5-pepito-grillo-.jpg

With this one, i get any special characters in block, just what i need, but need to skip the extension of the filename: 有了这个,我得到了块中任何特殊字符,正是我所需要的,但是需要跳过文件名的扩展名:

/[^\\w\\d]+/g Result: manuel-fernandex-Index-Prot-bla-otype-5-pepito-grillo-jpg /[^\\w\\d]+/g结果: manuel-fernandex-Index-Prot-bla-otype-5-pepito-grillo-jpg

With this one, i remove any special character except the filename but also leaving all the dots in the filename: 有了这个,我删除了除了文件名以外的任何特殊字符,还保留了文件名中的所有点:

[^\\d\\w\\_\\-\\.]+ Result: manuel-fernandex-Index-Prot.bla.otype-5-pepito-grillo.jpg [^\\d\\w\\_\\-\\.]+结果: manuel-fernandex-Index-Prot.bla.otype-5-pepito-grillo.jpg

Im very close but i cant find the final solution. 我非常接近,但我找不到最终的解决方案。

You may remove any chars other than word and dot chars with [^\\w.] and any dot not followed with 1+ non-dot chars at the end of the string: 您可以使用[^\\w.]除去单词和点字符以外的所有字符,并在字符串末尾删除不带1+非点字符的点。

filename = filename.replace(/(?:\.(?![^.]+$)|[^\w.])+/g, "-");

See the regex demo 正则表达式演示

Details 细节

  • (?: - start of a non-capturing group: (?: -非捕获组的开始:
    • \\.(?![^.]+$) - any dot not followed with 1+ non-dot chars at the end of the string \\.(?![^.]+$) -字符串末尾没有后跟1+个非点字符的任何点
    • | - or - 要么
    • [^\\w.] - any char other than a word char and a dot char [^\\w.] -除单词char和点字符外的任何char
  • )+ - end of the group, repeat 1 or more times. )+ -组结束,重复1次或更多次。

Another solution ( if extensions are always present ): split out the extension, run your simpler regex on the first chunk then join back: 另一个解决方案( 如果始终存在扩展名 ):拆分扩展名,在第一个块上运行更简单的正则表达式,然后重新加入:

 var filename = "manuel fernandex – Index Prot.bla.otype 5 (pepito grillo).jpg"; var ext = filename.substr(filename.lastIndexOf('.') + 1); var name = filename.substr(0, filename.lastIndexOf('.')); console.log(name.replace(/\\W+/g, "-") + "." + ext); 

Note your /[^\\w\\d]+/g and /\\W+/g are equal as \\w includes \\d . 注意您的/[^\\w\\d]+/g/\\W+/g等于\\w包括\\d

Or, if extensions are optional, split with the last dot, replace as in the previous solution, and join back: 或者,如果扩展名是可选的,请用最后一个点分隔,替换为先前的解决方案,然后重新加入:

 var filename = "manuel fernandex – Index Prot.bla.otype 5 (pepito grillo).jpg"; var parts = filename.split(/\\.(?=[^.]*$)/); parts[0] = parts[0].replace(/\\W+/g, "-"); console.log(parts.join(".")); 

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

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