Ricardo Albear
Ricardo Albear

Reputation: 516

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, '-');

Original filename: manuel fernandex – Index Prot.bla.otype 5 (pepito grillo).jpg

Target filename: 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

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

Im very close but i cant find the final solution.

Upvotes: 2

Views: 3874

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

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:

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
    • | - or
    • [^\w.] - any char other than a word char and a dot char
  • )+ - end of the group, repeat 1 or more times.

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.

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("."));

Upvotes: 5

Related Questions