While browsing, I came across this thread: Is there a regular expression to match non-English characters?. It provides a regex to remove foreign characters using the code snippet
str = str.replace(/[^\x00-\x7F]+/g, "");
.
My goal is slightly different - I want to retain these foreign characters while filtering out special characters, but still allowing '- _
. Essentially, I'm looking to allow single quotes, hyphens, underscores, and empty spaces.
So my question is, how can I modify the existing JavaScript regex to achieve this? Currently, it looks like this:
str = str.replace(/[^a-zA-Z0-9'-_ ]/g, "");
For instance, let's consider the character 'ü'. Unfortunately, simply adding it to the existing regex doesn't work:
str = str.replace(/[^a-zA-Z0-9'-_ ü]/g, "");