Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the non-digit character with JavaScript Regular Expression
In this tutorial, we will learn to find non-digit characters using JavaScript regular expressions. The \D metacharacter matches any character that is not a digit (0-9), including letters, spaces, and special characters.
RegExp is an object that specifies patterns used for search and replace operations on strings or input validation. RegExp was introduced in ES1 and is fully supported by all browsers.
Syntax
The syntax for matching non-digit characters is:
new RegExp("\D") // Constructor syntax
/\D/ // Literal syntax (recommended)
RegExp modifiers can be added for different behaviors:
- g - Global matching (find all matches)
- i - Case-insensitive matching
- m - Multiline matching
new RegExp("\D", "g") // With modifier
/\D/g // Literal with global modifier
Using match() to Find Non-Digit Characters
The match() method returns an array of all non-digit characters found in the string:
<!DOCTYPE html>
<html>
<body>
<h2>Finding Non-Digit Characters</h2>
<p id="result"></p>
<script>
let text = "200 students qualified in the exam out of 250";
let pattern = /\D/g;
let result = text.match(pattern);
document.getElementById("result").innerHTML =
"Non-digit characters: " + result.join(", ");
</script>
</body>
</html>
Handling No Matches
When no non-digit characters are found, match() returns null. Always check for this:
<!DOCTYPE html>
<html>
<body>
<h2>Checking for Non-Digit Characters</h2>
<p id="result"></p>
<script>
let text = "123456";
let pattern = /\D/g;
let result = text.match(pattern);
if (result === null) {
document.getElementById("result").innerHTML =
"No non-digit characters found";
} else {
document.getElementById("result").innerHTML =
"Non-digit characters: " + result.join(", ");
}
</script>
</body>
</html>
Replacing Non-Digit Characters
Use replace() to substitute non-digit characters with other characters:
<!DOCTYPE html>
<html>
<body>
<h2>Replace Non-Digit Characters</h2>
<p>Original: "200 students qualified out of 250"</p>
<p id="result"></p>
<script>
let text = "200 students qualified out of 250";
let pattern = /\D/g;
let result = text.replace(pattern, "*");
document.getElementById("result").innerHTML =
"After replacement: " + result;
</script>
</body>
</html>
Alternative Replacement Method
You can also use split() and join() to replace non-digit characters:
<!DOCTYPE html>
<html>
<body>
<h2>Split and Join Method</h2>
<p id="result"></p>
<script>
let text = "abc123def456ghi";
let pattern = /\D/g;
let result = text.split(pattern).join("-");
document.getElementById("result").innerHTML =
"Result: " + result;
</script>
</body>
</html>
Common Use Cases
- Input validation: Check if a string contains only digits
- Data cleaning: Remove or replace unwanted characters from numeric data
- Text processing: Extract digits by removing non-digit characters
- Form validation: Ensure phone numbers or IDs contain only digits
Conclusion
The \D regular expression is a powerful tool for finding and manipulating non-digit characters in JavaScript. Use it with methods like match(), replace(), and test() for effective string processing and validation.
