Allow foreign characters with regex while excluding special symbols

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

Answer №1

Dealing with characters in Unicode can be quite tricky as you have to decide whether to include a large number of Unicode letters or exclude several special characters. Essentially, the goal is to create a regex pattern that allows only characters falling under the general categories for Unicode letters (Lu, Ll, Lt, Lm, Lo).

While some regex flavors support these Unicode general categories directly, JavaScript unfortunately does not. However, it is still possible using the Unicode addon for the XRegExp library. Here's an example:

XRegExp.replace(text, "[^\\p{Ll}\\p{Lu}\\p{Lt}\\p{Lm}\\p{Lo}'\\- _]", '', 'all');

Alternatively, if you prefer constructing a detailed JavaScript regex, you can utilize the CSET JavaScript library. Here's how you can use it to create your regex:

var regex = /[\u0000-\u001f!-&(-....<truncated>...-\u3104\u312e-\u3130\u318f-㆟\u31b8-\u31ef㈀-㏿\u4db6-䷿ ...<truncated>... -・\uffbf-\uffc1\uffc8-\uffc9\uffd0-\uffd1\uffd8-\uffd9\uffdd-\uffff]|[\ud803-...\u179a]\](https://example.com)

After constructing the regex, you can simply use the replace method to remove unwanted characters from a string.

Edit: If you also want to allow numbers, you can modify the regex accordingly by including the Unicode general categories `Nd` and `Nl`. Here's a sample regex to achieve this:

var regex = /[\u0000-\u001f!-&(-....<truncated>... [-`{-・\uffbf-\uffc1\uffc8-\uffc9\uffd0-/:-@[-^`{-©«-´¶-¹»-¿×÷˂-˅˒-˟˥-˫˭˯-\u036f͵......西风]()|[\ud800-\ud83f\ud869-\udbff]/g;

Answer №2

In JavaScript, there is no built-in unicode character class for Regular expressions. However, you can manually include or exclude specific characters using the following method:

str = str.replace(/[!@#\$%\^&\*\(\)\{\}\?<>\+:;",\.\\]/g, "");

Alternatively, you can utilize a library like XRegExp

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Getting the value of an element using a string in JQuery

How can I achieve the following using JQuery? var test = "'#ISP'"; alert($(test).val()); I am receiving a "Syntax error, unrecognized expression." I believe I might be overlooking something here. Thank you in advance! ...

Refreshing the page causes JavaScript to fail loading

Recently, I encountered a puzzling error. Upon visiting this link The carousel fails to load properly near the bottom of the page. However, if you click on the logo or navigate back to the home page, it works fine. Additionally, performing a command + r ...

Dealing with CORS policy challenge

I am encountering an issue with Access to XMLHttpRequest at 'http://demo.equalinfotech.com/Iadiva/images/product/1634623781ladiva_barbara_01.glb' from origin 'http://localhost:8100' being blocked by CORS policy due to the absence of the ...

Posting an array using jQuery's AJAX feature

Consider the following JavaScript array structure: testarr = new Array(); testarr[0] = new Array(); testarr[0]["#FFFFFF"] = "White"; testarr[0]["#FFFFFF"] = new Array(); testarr[0]["#FFFFFF"]["#FFFFFA"] = "A"; testarr[0]["#FFFFFF"]["#FFFFFB"] = "B"; test ...

When the bounds are adjusted, removing markers in Vue Cookbook's Google Map

I've encountered an issue with clearing markers after updating new bounds on a map. Whenever I post a request with new bounds, new markers get added to the map while the old ones remain in place. This situation becomes quite awkward for me because the ...

Is there a way for me to access the information within these curly brackets [[]}?

I'm facing a challenge where I need to extract the ID from an API response that is formatted in a way unfamiliar to me. As a result, I'm unsure of how to retrieve the ID data from this response. (This is my initial query, so if it's unclear ...

What could be causing the appearance of a Firefox error message during the execution of a Protractor test?

Currently, I am conducting end-to-end testing on an AngularJS application using Protractor. Every time I execute a spec, Firefox launches and closes with a particular message appearing: After that, Firefox starts working properly and the specs run smooth ...

What is the best technique for evaluating different businesses' operating hours?

I am trying to compare the opening hours stored on my server with the current time. Here are the details: Start: 09.00 End: 00.30 The goal is to determine if the store is open or closed based on the current time. If the current time falls outside of t ...

Can I use a single component for all routes in NextJS?

Just starting out with NextJS and facing a simple problem: I'm wondering if it's possible to achieve the following setup using NextJS // with react-router-dom <Router> <> <Header /> <Switch> & ...

Sending the axios fetched property from the parent component to the child component results in the error message "TypeError: Cannot read property 'x' of undefined"

I've noticed that this question has been asked before, but none of the solutions provided seem to work for my situation. Parent component import axios from "axios"; import { useEffect, useState } from "react"; import Child from &q ...

Linking JavaScript on the client side to a NodeJS application on the server side

I am new to NodeJS and I am currently exploring the app structure. I have developed a basic app using Socket.IO and MongoJS, which functions as a tracking system that gathers variables from a client-side script and stores them in Mongo. This is how I envi ...

Differences between ES6 class static method and regular function

When working with NodeJS, I am planning to create some utility functions. I have two options in mind. The first option involves using the traditional approach: module.exports = { random: () => Math.random(), }; Alternatively, I could use an ES6 c ...

Is there a way to make my DIVS update their content dynamically like buttons do, without manually adding an onclick function in the HTML code?

I am currently customizing a WordPress theme that lacks the option for onclick events on div elements. However, I can assign classes and IDs to them. In my design, I have four spans in a row, and when each span is clicked, I intend for the corresponding p ...

The persistent Bulma dropdown glitch that refuses to close

In the project I'm working on, I have implemented a Bulma dropdown. While the dropdown functions correctly, I am facing an issue when adding multiple dropdowns in different columns with backend integration. When one dropdown is open and another is cli ...

Personalized Design Incorporating Sections and Sidebars

I need the aside to be positioned on the right side and the section on the left side, both centered in the space. Feel free to check out this link <!DOCTYPE html> <html> <head> <style> #main { width: 800px; margin: 0 auto; } ...

JavaScript unable to access elements during page loading

I am facing an issue with the following code: var coll = document.getElementsByClassName("collapsible"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); v ...

Permuting sentences to create intricate anagrams

I am faced with a task of creating the correct phrase for a sentence anagram using an array of nearly 2700 strings. The list consists of almost 100k words that could potentially fit. My goal is to combine these words in groups of 1, 2, and 3 words togethe ...

Executing password validation on login/register form using Node.js and EJS

In order to demonstrate a simple login page, I have created a form that requests typical information like username, password, etc. Additionally, it prompts the user to confirm their password, and if the lengths do not match, an error is triggered to notify ...

Update the array state based on the selection of checkboxes and user input in real-time

In my current project using react js, I am working on a UI development task where I need to create a dynamic table based on data fetched from an API. Each row in the table includes a checkbox and a text input field that are dynamically generated. My goal i ...

Avoiding overlapping in setTimeOut when using jQuery.hover()

Looking to trigger an effect one second after the page loads, and then every 3 seconds in a loop. When the user hovers over a specific element with the ID #hover, the effect should pause momentarily. It should then resume 2 seconds after the user stops hov ...