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.).I
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.JavaScriptlet regex = /[^abc]/g; let str = "abcdefg"; let matches = str.match(rege
2 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 a
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.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 [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