JavaScript Regular Expressions (RegExp) Examples
Last Updated :
27 Nov, 2024
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 Word
The pattern /hello/
matches the string "hello" in the input string "hello world".
JavaScript
const p = /hello/;
const t = "hello world";
console.log(p.test(t));
2. Match Any Digit
The pattern \d matches any digit from 0 to 9.
JavaScript
const p = /\d/;
const t = "Order number 123";
console.log(p.test(t));
3. Match Any Non-Digit
The pattern \D matches any non-digit character.
JavaScript
const p = /\D/;
const t = "Order number 123";
console.log(p.test(t));
4. Match Whitespace
The pattern \s matches whitespace characters, including spaces, tabs, and line breaks.
JavaScript
const p = /\s/;
const t = "Hello world";
console.log(p.test(t));
5. Match a Specific Pattern (e.g., "ABC")
The pattern /abc/ matches the substring "abc" in the input string "alphabet".
JavaScript
const p = /abc/;
const t = "alphabet";
console.log(p.test(t));
6. Match a Word Boundary
The pattern \bcat\b matches "cat" only if it is a separate word. Since "cat" is part of "catfish", the match fails.
JavaScript
const p = /\bcat\b/;
const t = "catfish";
console.log(p.test(t));
7. Match an Email Format
The pattern matches a typical email format.
JavaScript
8. Match a Phone Number Format
The pattern matches a phone number in the format "123-456-7890".
JavaScript
const p = /\d{3}-\d{3}-\d{4}/;
const t = "123-456-7890";
console.log(p.test(t));
9. Match a Date in MM/DD/YYYY Format
The pattern matches dates formatted as "MM/DD/YYYY".
JavaScript
const p = /\b\d{2}\/\d{2}\/\d{4}\b/;
const t = "05/12/2022";
console.log(p.test(t));
10. Match a Hexadecimal Color Code
This pattern matches both 6-digit and 3-digit hexadecimal color codes.
JavaScript
const p = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
const t = "#a1b2c3";
console.log(p.test(t));
11. Match a URL Format
The pattern matches URLs that start with "http" or "https".
JavaScript
const p = /https?:\/\/(www\.)?\S+\.\S+/;
const t = "https://example.com";
console.log(p.test(t));
12. Match Only Alphanumeric Characters
This pattern matches strings containing only alphanumeric characters.
JavaScript
const p = /^[a-zA-Z0-9]+$/;
const t = "user123";
console.log(p.test(t));
13. Match a Password with at Least 8 Characters, Including a Number and a Special Character
The pattern enforces a password rule requiring at least 8 characters, a number, and a special character.
JavaScript
const p = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
const t = "Pass@123";
console.log(p.test(t));
14. Match a 5-Digit or 9-Digit ZIP Code
This pattern matches US ZIP codes in 5-digit or 9-digit formats.
JavaScript
const p = /^\d{5}(-\d{4})?$/;
const t = "12345-6789";
console.log(p.test(t));
15. Match a String That Starts with a Specific Word
The pattern ^Hello matches any string that starts with "Hello".
JavaScript
const p = /^Hello/;
const t = "Hello world!";
console.log(p.test(t));
16. Match a String That Ends with a Specific Word
The pattern world$ matches strings ending with "world".
JavaScript
const p = /world$/;
const t = "Hello world";
console.log(p.test(t));
17. Match Any Character Except Line Breaks
The dot . matches any character except line breaks.
JavaScript
const p = /./;
const t = "Hello!";
console.log(p.test(t));
18. Match a String with Only Uppercase Letters
The pattern matches strings containing only uppercase letters.
JavaScript
const p = /^[A-Z]+$/;
const t = "HELLO";
console.log(p.test(t));
19. Match a Repeated Character (e.g., "aaa")
This pattern matches the repeated character "a" three times.
JavaScript
const p = /(a)\1\1/;
const t = "aaa";
console.log(p.test(t));
20. Match Only Lowercase Letters
The pattern matches strings containing only lowercase letters.
JavaScript
const p = /^[a-z]+$/;
const t = "hello";
console.log(p.test(t));
Similar Reads
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.).
4 min read
JavaScript RegExp [^abc] Expression
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. [GFGTABS] JavaScript let regex = /[^abc]/g; let str = "abcdefg"; let ma
3 min read
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
2 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. [GFGTABS] JavaScript let regex = /(cat|dog)/g; let str = "I have a cat and a dog."; let matches = str
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 string [GFGTABS] JavaScript const regex = /[^0-9]/g; const str =
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. [GFGTABS] JavaScript let regex = /[0-9]/g; let str = "abc123xyz"; let matches = str.match(regex); console.log(matches);
3 min read
JavaScript Regular Expressions Coding Practice Problems
Regular expressions (regex) in JavaScript provide a powerful way to search, match, and manipulate text patterns. They are widely used in form validation, data extraction, text filtering, and pattern recognition. This curated list of regex-based coding problems is categorized into easy, medium, and h
1 min read
How to clone a given regular expression in JavaScript ?
In this article, we will know How to clone a regular expression using JavaScript. We can clone a given regular expression using the constructor RegExp(). The syntax of using this constructor has been defined as follows:- Syntax: new RegExp(regExp , flags) Here regExp is the expression to be cloned a
2 min read
How to Access Matched Groups in a JavaScript Regular Expression ?
Here are the different methods to access matched groups in JavaScript regular Expression(RegExp). 1. Using exec() MethodThe exec() method returns an array with the entire match and captured groups, which you can access by their index in the result array. [GFGTABS] JavaScript let s = "The price
3 min read
How useful is learning regular expressions in JavaScript ?
Regular expressions (RegExp) are an important skill for JavaScript developers that helps in text manipulation, validation, and extraction. Whether youâre working on form validations, file handling, or log parsing, mastering regular expressions can simplify your code and improve productivity. Why Lea
3 min read