Open In App

JavaScript RegExp [^0-9] Expression

Last Updated : 28 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The RegExp [^0-9] Expression in JavaScript is used to search any digit which is not between the brackets. The character inside the brackets can be a single digit or a span of digits. 

Example: Finding non-digit characters from given string

JavaScript
const regex = /[^0-9]/g;
const str = "Hello123Geeks";

const result = str.match(regex);
console.log(result);

Output
[
  'H', 'e', 'l', 'l',
  'o', 'G', 'e', 'e',
  'k', 's'
]

Syntax

/[^0-9]/ 
// or
new RegExp("[^0-9]")

Syntax with modifiers

/[^0-9]/g 
// or
new RegExp("[^0-9]", "g")

Example 1: Searching the digits which are not present between [0-4] in the given string. 

JavaScript
let str = "123456790";
let regex = /[^0-4]/g;
let match = str.match(regex);

console.log("Found " + match.length
    + " matches: " + match);

Output
Found 4 matches: 5,6,7,9

 Example 2: Searching the digits which are not present between [0-9] in the given string and replaces the characters with hash(#). 

JavaScript
let str = "128@$%";
let replacement = "#";
let regex = new RegExp("[^0-9]", "g");
let match = str.replace(regex, replacement);

console.log("Found " + match.length
    + " matches: " + match);

Output
Found 6 matches: 128###

 Supported Browsers

  • Chrome
  • Edge
  • Safari
  • Firefox
  • Opera

We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  


Next Article

Similar Reads