JavaScript RegExp [^abc] Expression
Last Updated :
11 Jul, 2025
The [^abc] expression in JavaScript regular expressions is a negated character set. It matches any character except those specified inside the brackets. For example, [^abc] matches any character that is not a, b, or c.
JavaScript
let regex = /[^abc]/g;
let str = "abcdefg";
let matches = str.match(regex);
console.log(matches);
Output[ 'd', 'e', 'f', 'g' ]
The pattern [^abc] excludes a, b, and c, matching only d, e, f, and g.
Syntax:
/[^characters]/
- [^...]: Matches any character not listed inside the brackets.
- The g flag ensures the search continues globally across the string.
Key Points
- The [^...] expression negates the character set inside the brackets.
- Matches a single character that is not in the defined set.
- Works with ranges (e.g., [^a-z] matches anything that is not a lowercase letter).
Real-World Examples
1. Matching Non-Specified Characters
JavaScript
let regex = /[^aeiou]/g;
// Matches all non-vowel characters
let str = "hello world";
let matches = str.match(regex);
console.log(matches);
Output[
'h', 'l', 'l',
' ', 'w', 'r',
'l', 'd'
]
Here, [^aeiou] matches any character that is not a vowel.
2. Excluding Specific Digits
JavaScript
let regex = /[^123]/g;
let str = "123456789";
let matches = str.match(regex);
console.log(matches);
Output[ '4', '5', '6', '7', '8', '9' ]
The pattern [^123] excludes the digits 1, 2, and 3, matching the remaining numbers.
3. Filtering Out Specific Characters
JavaScript
let regex = /[^a-zA-Z]/g;
// Matches all non-alphabetic characters
let str = "Code123!";
let result = str.replace(regex, "");
console.log(result);
The [^a-zA-Z] pattern removes all characters that are not alphabets.
4. Validating Input
JavaScript
let regex = /[^a-zA-Z0-9]/;
let username = "User_123";
if (regex.test(username)) {
console.log("Invalid username. Contains special characters.");
} else {
console.log("Valid username.");
}
OutputInvalid username. Contains special characters.
The [^a-zA-Z0-9] pattern detects any special character in the username.
5. Excluding Ranges
JavaScript
let regex = /[^0-9]/g;
// Matches all non-digit characters
let str = "abc123xyz";
let matches = str.match(regex);
console.log(matches);
Output[ 'a', 'b', 'c', 'x', 'y', 'z' ]
Here, [^0-9] matches any character that is not a digit.
Common Patterns Using [^...]
- Exclude Digits: Matches all non-digit characters.
/[^0-9]/g
- Exclude Letters: Matches all non-alphabetic characters.
/[^a-zA-Z]/g
- Exclude Whitespace: Matches all non-whitespace characters.
/[^\s]/g
- Exclude Specific Characters: Matches any character except a, b, or c.
/[^abc]/g
Why Use [^...]?
- Flexible Exclusions: Allows precise control over characters you don’t want to match.
- Input Validation: Ensures input only includes allowed characters.
- String Cleaning: Easily remove unwanted characters like digits or special symbols.
Conslusion
The [^abc] expression is a powerful way to define what not to match, making it a critical tool for complex string processing and validation tasks in JavaScript.
Recommended Links:
Similar Reads
JavaScript RegExp [abc] Expression The RegExp [abc] Expression in JavaScript is used to search any character between the brackets. The character inside the brackets can be a single character or a span of characters.[A-Z]: It is used to match any character from uppercase A to Z.[a-z]: It is used to match any character from lowercase a
2 min read
JavaScript RegExp [^0-9] Expression 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 stringJavaScriptconst regex = /[^0-9]/g; const str = "Hello123Geeks"
2 min read
JavaScript RegExp [0-9] Expression The [0-9] expression in JavaScript regular expressions matches any single digit between 0 and 9. It is a character class used to represent a range of numeric characters.JavaScriptlet regex = /[0-9]/g; let str = "abc123xyz"; let matches = str.match(regex); console.log(matches);Output[ '1', '2', '3' ]
3 min read
JavaScript RegExp (x|y) Expression The (x|y) expression in JavaScript regular expressions is used to match either x or y. It acts as an OR operator in regular expressions, allowing you to specify multiple patterns to match.JavaScriptlet regex = /(cat|dog)/g; let str = "I have a cat and a dog."; let matches = str.match(regex); console
2 min read
JavaScript RegExp (Regular Expression) A regular expression is a special sequence of characters that defines a search pattern, typically used for pattern matching within text. It's often used for tasks such as validating email addresses, phone numbers, or checking if a string contains certain patterns (like dates, specific words, etc.).I
4 min read
JavaScript Regular Expressions (RegExp) Examples Regular Expressions in JavaScript provide robust pattern-matching capabilities that are useful for validating input, searching and replacing text, and more. Here, we will explore various examples demonstrating the utility of RegExp in JavaScript.1. Match a WordThe pattern /hello/ matches the string
3 min read