The answers to the question you linked show using negated classes. The classes list the characters to keep. So just add the other characters you want to keep to those classes. (Spanish uses relatively few diacritical marks, so it's basically ñ
and a couple of accented vowels.
For instance, using this answer as a starting point:
str.replace(/[^\x00-\x7F]/g, "");
and adding ñ
, á
, é
, í
, ó
, ú
str.replace(/[^\x00-\x7Fñáéíóú]/g, "");
If you have it available on your target system, you may want to normalize the string to NFC form first, so that if accents are written with combining marks (rather than the single code point for an accented letter), those get handled:
if (str.normalize) {
str = str.normalize();
}
str.replace(/[^\x00-\x7Fñáéíóú]/g, "");
Otherwise, you might want to allow for the combining accents. That would complicate the regular expression.
Here's an exmaple of a string with a combining acute accent and what the regex above does to it without and with normalization:
if (!String.prototype.normalize) {
console.log("This host doesn't support the normalize method");
} else {
const str = "Buenos di\u0301as";
console.log("string:", str);
console.log(
"without normalization:",
str.replace(/[^\x00-\x7Fñáéíóú]/g, "")
);
console.log(
"with normalization: ",
str.normalize().replace(/[^\x00-\x7Fñáéíóú]/g, "")
);
}
Notice how in the "without" case, the combining mark was removed, and "dias" was misspelled as a result.