What Does the RegExp D Metacharacter Do in JavaScript
This write-up demonstrates the working of the “\D” metacharacter in the RegExp of JavaScript.
What Does the RegExp \D Metacharacter Do in JavaScript?
The “\D” metacharacter finds the non-digit characters in JavaScript. It works on the initialized regexp pattern and returns the non-digit characters present inside it.
Syntax (Basic)
In the above syntax:
- /(Forward-slash): Indicates the defined regular expression boundaries.
- \(Backslash): Removes the backslash for treating the specified character as a metacharacter.
- D: It refers to the metacharacter that matches the non-digit characters.
Syntax (With Modifiers)
In this syntax:
- g(global): Searches the initialized pattern globally and does not stop after the first match.
- i(case-sensitive): Matches character by ignoring the case sensitivity.
- m(multiple): Specifies the multiline search and is limited between “^(start of the string)” and “$(end of the string)”.
Syntax (With RegExp() Constructor)
In this syntax:
- new: A keyword act as an operator for the creation of an object from a constructor.
- RegExp(): Denotes the constructor that takes the “\\D” metacharacter as its parameter/argument.
Let’s implement the above syntaxes one by one to demonstrate the practical implementation of the “\D” metacharacter.
HTML Code
First, have a look at the following HTML code:
In the above code snippet:
- Define a subheading via the “<h2>” tag.
- Next, the “<p>” tag specifies the initialized string that needs to be evaluated.
- After that, a button is added with the help of the “<button>” tag with an “ondblclick” event. It redirects the specified event to the function “search()” which will be triggered upon the button double-click.
- Lastly, the “<p>” tag includes an empty paragraph with an assigned id “find” to display the matched non-digit characters.
Note: This HTML code will be followed throughout this write-up.
Example 1: Use the “\D” Metacharacter to Find Non-Digit Characters Based on Modifier Syntax (/\D/g)
This example utilizes the “/\D” metacharacter to search the non-digit characters globally using an additional “g(global)” modifier.
JavaScript Code
Consider the stated JavaScript code:
function search() {
var string = "Linux12345@...%";
var pattern = /\D/g;
var result = string.match(pattern);
document.getElementById("find").innerHTML = result;
}
</script>
In the above code block:
- Define a function named “search()”.
- The function definition initializes the stated “string” comprising the alphanumeric value.
- Next, the variable “pattern” specifies the “/\D/g” metacharacter to search the non-digit characters globally from the initialized string.
- After that, apply the “match()” method to match the non-digit characters using the “D” metacharacter specified in the “pattern” variable.
- Lastly, the “getElementById()” method approaches the included empty paragraph via its id “find” to append and display the searched non-digit characters via the “innerHTML” property.
Output
The output returns the matched “non-digit characters” from the initialized string upon button double-click.
Example 2: Use the “\D” Metacharacter to Find Non-Digit Characters Based on “RegExp() Constructor” Syntax
This example uses the “new RegExp(“\\D”, “g”)” constructor syntax to show the working of the “\D” metacharacter practically:
JavaScript Code
Follow the given JavaScript code:
function search() {
var string = "Linux12345@...!";
var pattern = new RegExp("\\D", "g");
var result = string.match(pattern);
document.getElementById("find").innerHTML = result;
}
</script>
The above code lines, apply the “new RegExp(“\\D”, “g”)” syntax to globally match the non-digit characters from the initiated string at the button double-click.
Output
As analyzed, the outcome of “new RegExp(“//D”, “g”)” is identical to the “/\D/g”.
Conclusion
In JavaScript, the “\D” metacharacter in the regexp pattern finds the non-digit characters from the string and returns them. It stops the matching process at the first digit match by default without completing the search from the whole string. However, the “g(global)” modifier can be used to search the string globally based on the pattern. It is opposite to the lowercase “\d” metacharacter that matches the digits instead. This post provided the working and functionality of the RegExp “\D” metacharacter in JavaScript.
Source: linuxhint.com