This post explains the working of the “\d” metacharacter in the RegExp of JavaScript.
What Does “d” Metacharacter Do in RegExp of JavaScript?
The “\d” metacharacter works on single numeric values. In the case of multiple digits, it matches every single digit and displays the whole value separated by a comma “,” automatically.
Syntax(Basic)
In the above syntax:
- /(Forward-slash): Specifies the boundaries of the regular expression and is equivalent to /[\d]/.
- \(Backslash): Escapes the backslash and treats the next mentioned character as a metacharacter.
- d: Matches the digits from “0-9”.
Syntax(With Modifiers)
In the above-given syntax:
- g(global): Searches globally and finds all the matches. It does not stop after the first match.
- i(case-sensitive): Ignores the case sensitivity.
- m(multiple): Species the multiline search and is only restricted at “^(start of the string)” and “$(end of the string)”.
Syntax(With RegExp() Constructor)
The “\d” metacharacter can also be utilized as a RegExp object with the “RegExp()” constructor:
In this syntax:
- new: Represents the operator or keyword that creates an object from a constructor function.
- RegExp(): Denotes the constructor that supports the “\\d” metacharacter in the form of the first parameter/argument.
Example 1: Applying the “d” Metacharacter to Find Digits From a String Based on Modifier Syntax (/\d/g)
In this example, the “/\d” metacharacter can be used with the “g(global)” modifier to match the digits from the string.
HTML Code
Let’s overview the following HTML code:
<p> String: Linuxhint12345 </p>
<button ondblclick="find()">Double Click</button>
<p id="search"></p>
In the above HTML code:
- The “<h2>” tag is specified for defining the first subheading.
- The “<p>” tag creates a paragraph to display the string.
- The “<button>” tag creates a button having an “ondblclick” event redirecting to the function “find()” which will be triggered upon the button double-click.
- After that, the “<p>” denotes the second empty paragraph assigned an id “search” to display the searched digits.
JavaScript Code
Now, let’s move on to the JavaScript code:
function find() {
var str = "Linuxhint12345";
var pattern = /\d/g;
var result = str.match(pattern);
document.getElementById("search").innerHTML = result;
}
</script>
In the above JavaScript code:
- Define a function named “find()”.
- In its definition, initialize the stated string variable comprising digits in it.
- In the next step, the variable “pattern” defines the syntax of the metacharacter “\d” with an additional “g” global search flag/modifier to find the digits globally from the initialized string.
- Now, apply the “match()” method to match the digits from the string with the help of the discussed metacharacter.
- Lastly, the “document.getElementById()” method accesses the paragraph via its id “search” to append and display the searched digits.
Output
The output displays the searched “digits” from the provided string upon the button double-click accordingly.
Example 2: Applying the “d” Metacharacter to Find Digits From a String Based on “RegExp() Constructor” Syntax
The “new RegExp(“\\W”, “g”)” syntax can also be utilized to find the digits. It returns the same output as from the “/\d\g” syntax. Let’s see how it can be used in the JavaScript code.
Note: The HTML code is the same as in Example 1.
JavaScript Code
Let’s overview the following JavaScript code:
function find() {
var str = "Linuxhint12345";
var pattern = new RegExp("\\d", "g");
var result = str.match(pattern);
document.getElementById("search").innerHTML = result;
}
</script>
In the above lines of code, the “pattern” variable specifies the “new RegExp(“\\d”, “g”)” syntax to locate and return the digits from the initialized string globally upon button double-click.
Output
As analyzed, the outcome is identical in this case as well.
Conclusion
In JavaScript, the built-in “\d” metacharacter is beneficial for finding the digits between “0-9”. By default, it stops at the first digit match without completing the search from the whole string. For this purpose, the “g(global)” modifier can be used with it to complete the search appropriately. This blog provided a detailed demonstration of the “d” metacharacter in RegExp of JavaScript.