Open In App

JavaScript Regular Expressions (RegExp) Examples

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

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)); 

Output
true

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));

Output
true

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)); 

Output
true

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)); 

Output
true

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)); 

Output
false

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));

Output
false

7. Match an Email Format

The pattern matches a typical email format.

JavaScript
const p = /\S+@\S+\.\S+/;
const t = "[email protected]";
console.log(p.test(t)); 

Output
true

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)); 

Output
true

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));

Output
true

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));

Output
true

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));

Output
true

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));  

Output
true

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));

Output
true

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));

Output
true

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));

Output
true

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));

Output
true

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));

Output
true

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));

Output
true

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));

Output
true

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));

Output
true

Next Article
Article Tags :

Similar Reads