Does \d exclusively match digits from 0 to 9?

From my understanding, the \d in JavaScript should match non-english digits like ۱۲۳۴۵۶۷۸۹۰, but it appears to not be functioning correctly.

You can check out this jsFiddle for more details: http://jsfiddle.net/xZpam/

Is this normal behavior or is something off?

Answer №1

It appears that JavaScript lacks support for this feature, among other limitations in the language regarding RegExp. However, there exists a library known as XRegExp which includes a unicode addon. This addon allows for unicode support using the \p{} category definition. For instance, by utilizing \p{Nd} instead of \d, you can match digits:

<script src="xregexp-all.js" type="text/javascript"></script>
<script type="text/javascript">
    var englishDigits = '123123';
    var nonEnglishDigits = '۱۲۳۱۲۳';

    var digitsPattern = XRegExp('\\p{Nd}+');
    if (digitsPattern.test(nonEnglishDigits)) {
        alert('Non-english using xregexp');
    }

    if (digitsPattern.test(englishDigits)) {
        alert('English using xregexp');
    }
</script>

UPDATE:

Replaced \p{N} with \p{Nd} as it seems that \d is similar to \p{Nd} in non-ECMA Script Regex engines. Credit goes to Shervin for bringing this to light. Check out this fiddle by Shervin as well.

Answer №2

Did you know that JavaScript does not have native support for Unicode regex matching? This limitation is actually quite common across many programming languages.

If you want to learn more about using Unicode in regular expressions, check out this helpful resource:

Answer №4

The character \d is the same as using [0-9], as explained on MDN website.

Answer №5

According to the MDN documentation, you can use the RegEx Test to match a digit character in the basic Latin alphabet.

This is equivalent to matching characters within the range [0-9].

Answer №6

Identifies a numeric character. Essentially the same as [0-9].

For instance, using /\d/ or /[0-9]/ successfully identifies '6' in the phrase "Room 6 is where you'll find your keys."

Extracted from Wikipedia

Answer №7

Absolutely, it is perfectly normal and accurate that \d specifically matches the Ascii digits 0 through 9. The definitive source for this information can be found in the ECMAScript standard. While not the simplest of reads, clause 15.10.2.12 (CharacterClassEscape) explicitly states that \d represents "the ten-character set encompassing the numbers 0 to 9".

Answer №8

Absolutely, the use of \d does not match properly with non-English numbers in JavaScript. However, similar to other peculiar aspects of JavaScript, you can still verify non-English numbers (such as Persian numbers) in JavaScript by utilizing something similar to the code provided below:

/[۰, ۹]/.test("۱۲۳۴۵۶۷۸۹۰"); //true

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

Tips for increasing visibility for your Google Analytics Embed API Custom Components

I recently tried to incorporate some code I found at the following link: After downloading the files view-selector2 and date-range-selector, I saved them in my local directory. I made a modification to the code: var accountSummaries = require(['&ap ...

What is the best way to manage a significant volume of "business" data or objects within my JavaScript project?

I specialize in working with AngularJs and have a factory that provides services related to buildings. I am managing a large number of buildings (around 50-60) with multiple properties and sub-properties associated with each one (approximately 15-20, some ...

Updating a query parameter with jQuery

Looking for a way to modify the value of a query parameter in a URL using jQuery. There are two example URLs: www.domain.com/?id=10&name=xxx and www.domain.com/?name=xxx&id=10 I want to update the id parameter to something like www.domain.com/ ...

How can you stop data URI from being cached as an image source?

I am facing an issue where I have an img-tag with a data-image base64 URI as the source. Browsers tend to cache this source, which is causing problems for me. If it were a normal URL, I could easily prevent caching by adding a random query-parameter value. ...

How do I dynamically incorporate Tinymce 4.x into a textarea?

I am encountering a small issue when trying to dynamically add tinymce to a textarea after initialization. tinymce.init({ selector: "textarea", theme: "modern", height: 100, plugins: [ "advlist autolink image lists charmap print p ...

Analyzing the functionality of Express with Mocha and Chai

Currently facing an issue with testing my express server where I am anticipating a 200 response. However, upon running the test, an error occurs: Test server status 1) server should return 200 0 passing (260ms) 1 failing 1) Test server statu ...

The $resources headers have not been updated

My objective is to include a header with each HTTP request for an ngResource (specifically, an auth token). This solution somewhat accomplishes that: app.factory('User', ['$resource','$window', function($resource,$window,l ...

Sorry, the provided text is already unique as it is an error message

I'm currently using the react-highlight-words package to highlight text inputted into a textbox After checking out the react-highlight-words documentation, I noticed that they are using searchWords as an array. https://www.npmjs.com/package/react-high ...

What is the best way to insert a JavaScript variable into a filter while using ng-repeat?

I'm currently working on a situation where I have the following code: <div ng-repeat="i in MediaFeedItems | filter: { category: 'userSelect' }" class="mediafeed--item"> This specific code snippet is responsible for iterating through ...

Error encountered in Node.js: Attempting to modify headers after they have already been sent to the client

When attempting to create a login function and sending post requests using Postman, everything works fine with the correct email and password. However, if I try to send the wrong password, I encounter an error message stating that either the email or passw ...

How to retrieve the chosen option from a drop-down menu created within a loop in a React application

I am seeking guidance on the best way to create multiple drop-down menus (<select>) using a loop, so that I can retrieve their selected values using a button. I have attempted to replicate what is currently in my code, hoping this information is suff ...

What is preventing me from obtaining the select and input values?

I'm currently facing an issue where I am unable to retrieve the values of customInput and customSelect and store them in the state. The challenge arises when trying to integrate these components into a react dashboard material-ui setup. Strangely, whe ...

Insufficient Resources Error (net::ERR_INSUFFICIENT_RESOURCES) encountered while executing jQuery script with multiple ajax requests for 2 minutes

Upon initially loading the code below, everything seems to be functioning smoothly with the dayofweek and hourofday functions. However, shortly thereafter, the browser (Chrome) freezes up and displays the error message: net::ERR_INSUFFICIENT_RESOURCES. Thi ...

placeholder for dropdown selection in Vue.js version 2.0.0

Currently, I am in the process of developing a web application using vuejs 2.0. In order to create a straightforward select input, I employed the following code: <select v-model="age"> <option value="" disabled selected hidden>Select Age&l ...

How to pass an item as a parameter to a computed property in Vue.js, and have it return a sorted child array within a

After spending some time searching on Google, I am still struggling to find a solution for this issue. I have a list of "Intents" that contain nested lists of "Entities" created using v-for loops. The Intents are already computed, but now I need to dynam ...

Sending a PDF document and JSON data via an AJAX POST request

I'm attempting to send a PDF document along with some JSON data in string format using an AJAX method in jQuery to an ASP.NET WEB API (2). Below are my attempts that are not working: JAVASCRIPT: // Obtaining the authorization token works fine var hea ...

Issue with Javascript Promise causing failure to populate list with objects

app.get('/zones/:id/experiences', function(req,res) { var zone_key = req.params.id; var recent = []; var ref = firebase.database().ref('participants/'+zone_key+'/experiences'); ref.on("value", function(snapshot) { ...

Instructions for using Selenium to access the "stats for nerds" option on a YouTube video by right-clicking

I am currently working on a program that has the ability to block YouTube ads and keep track of the blocked Ad Video IDs. To achieve this, our selenium program needs to simulate a right click on the video to open the video menu and then select "stats for ...

What is the best way to set up distinct Jest test environments for React Components and Backend API routes within NextJs?

In the realm of testing with NextJS, Jest comes into play effortlessly, complemented by React Testing Library for frontend testing. Interestingly, Jest can also be utilized to test backend code. Currently, I am incorporating a library in my API routes tha ...

What is the function of async in Next.js when triggered by an onClick

Need help with calling an async function pushData() from a button onClick event async function pushData() { alert("wee"); console.log("pushing data"); try { await query(` //SQL CODE `); console.log("Done&quo ...