I'm trying to remove accents and special characters except dash(-
), underline(_
) and preserve the extension.
For example:
ÁÉÍÓÚáéíóúâêîôûàèìòùÇãç.,~!@#$%&_-12345.png
to:
AEIOUaeiouaeiouaeiouCac_-12345.png
I came to this result, but the problem is that it's ignoring all dots. I need to ignore only the last occurrence to preserve the extension from filename.
"ÁÉÍÓÚáéíóúâêîôûàèìòùÇãç.,\~!@#$%&_-12345.png".normalize('NFD').replace(/\[^a-zA-Z0-9-_\]/g, "")
I already tried negative look behind like this:
/[^a-zA-Z0-9-]+(?<!\.)/g
using this reference, but I didn't have success.
"ÁÉÍÓÚáéíóúâêîôûàèìòùÇãç.,~!@#$%&-12.34.5.png".normalize('NFD').replace(/[^a-zA-Z0-9-]+(?<!\.)/g, '')
If I have more than a dot in this case it only removes the first .
.
.normalize('NFD').replace(/[^a-zA-Z0-9-._]/g, '')
? It looks like it does what you want without overcomplicating[^\w.-]+|\.(?=[^.]*\.)