Regular Expressions Cheat Sheet
seen from United States
seen from Russia
seen from China
seen from China

seen from Germany
seen from China
seen from China

seen from Ireland

seen from United States

seen from Türkiye

seen from United States
seen from United States
seen from Indonesia
seen from United States

seen from United States
seen from Israel

seen from Indonesia
seen from United States
seen from United States
seen from United States
Regular Expressions Cheat Sheet
JavaScript Regex Made Easy: Tips, Tricks, and Examples
Regular Expressions, commonly known as Regex, are a powerful feature in JavaScript that allow developers to search, match, and manipulate strings efficiently. Whether you’re validating email addresses, phone numbers, or cleaning up user input, understanding Regex can save a lot of time and make your code more robust.
In this blog post, we’ll break down Regex in JavaScript, explain key concepts, share useful tips, and provide practical examples for beginners and intermediate developers.
What is Regular Expression (Regex)?
A regular expression is a sequence of characters that defines a search pattern. Think of it as a way for JavaScript to identify specific text patterns in strings. Regex is widely used for validation, search, replace operations, and string manipulation.
In JavaScript, Regex can be used in two ways:
Using regex literals
let pattern = /hello/;
Using the RegExp constructor
let pattern = new RegExp("hello");
Both methods create a Regex object that can be used with JavaScript string methods like .test(), .match(), .replace(), and .search().
Common Regex Methods in JavaScript
1. .test()
Checks if a pattern exists in a string and returns true or false.let pattern = /world/; console.log(pattern.test("Hello world")); // true
2. .match()
Returns an array of matches found in the string.let text = "I love JavaScript"; let matches = text.match(/JavaScript/); console.log(matches[0]); // "JavaScript"
3. .replace()
Replaces matched patterns in a string with a new value.let text = "I love cats"; let newText = text.replace(/cats/, "dogs"); console.log(newText); // "I love dogs"
4. .search()
Returns the index of the first match or -1 if not found.let text = "Learn JavaScript"; console.log(text.search(/JavaScript/)); // 6
Basic Regex Patterns
Here are some common patterns used in JavaScript Regex: Pattern Description Example . Matches any single character /a.b/ matches "acb" ^ Start of string /^Hello/ matches "Hello World" $ End of string /World$/ matches "Hello World" \d Matches any digit [0-9]/\d/ matches "7" \D Non-digit /\D/ matches "a" \w Word character [a-zA-Z0-9_]/\w/ matches "A" \W Non-word character /\W/ matches "!" \s Whitespace /\s/ matches " " \S Non-whitespace /\S/ matches "a" + One or more occurrences /a+/ matches "aa" * Zero or more occurrences /a*/ matches "aaa" ? Zero or one occurrence /a?/ matches "a" or "" [abc] Matches any character a, b, or c /[abc]/ matches "a" [^abc] Matches anything except a, b, c /[^abc]/ matches "d"
Practical Examples
1. Email Validation
let emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}$/; console.log(emailPattern.test("[email protected]")); // true
2. Phone Number Validation
let phonePattern = /^\d{10}$/; console.log(phonePattern.test("9876543210")); // true
3. Replacing Multiple Spaces
let text = "JavaScript is awesome"; let cleanedText = text.replace(/\s+/g, " "); console.log(cleanedText); // "JavaScript is awesome"
4. Extracting Numbers from a String
let str = "There are 15 apples and 20 oranges"; let numbers = str.match(/\d+/g); console.log(numbers); // ["15", "20"]
5. Case-Insensitive Matching
let pattern = /javascript/i; console.log(pattern.test("I love JavaScript")); // true
Tips & Tricks for JavaScript Regex
Use the g flag for global search – finds all matches, not just the first.
let text = "apple banana apple"; let matches = text.match(/apple/g); console.log(matches); // ["apple", "apple"]
Use the i flag for case-insensitive matching – ignores uppercase/lowercase differences.
Escape special characters using \ if you want to match characters like . or ?.
let pattern = /\./; // matches a dot
Test regex with online tools – Tools like regex101.com help visualize matches and understand patterns.
Keep patterns readable – Avoid overly complex regex; break down tasks into smaller regex patterns if needed.
When to Use Regex
Regex is extremely useful when you want to:
Validate user input (emails, passwords, phone numbers)
Search and extract patterns from text
Replace or clean strings efficiently
Analyze logs or text data
However, avoid overusing Regex for very complex operations as it can become hard to read and maintain.
Conclusion
JavaScript Regular Expressions are a powerful tool that every developer should learn. From validating forms to extracting data from strings, Regex makes string manipulation efficient and flexible.
By understanding basic patterns, flags, and methods like .test(), .match(), and .replace(), beginners can handle most real-world scenarios. Remember to practice with examples, use online regex testers, and gradually explore advanced patterns.
Regular Expressions in GA4
Regular expression for strong password validation : Here is the pattern :👇 var pattern = /^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@&]).*$/; #codewithsuri #Jquery #html #css #regularexpressions (at Link in Bio) https://www.instagram.com/p/CL6t262ggXd/?igshid=qe95yyx8p3er
Python - YouTube
Check out these cool videos done by 9 years old kid
Hey everyone.. sorry I've been MIA on my designing and such.. I've just been real restructuring and taking a #WebDevelopment class amongst others. I'm 20% in... Currently on chapter 4 #JavaScript #RegularExpressions 6 more lessons, a quiz then I create a Math Game. Up next: Chapter 5 #JQuery. Follow my brand page @chucks8heels (at Ms. Parkerazzi)
Hey everyone.. sorry I've been MIA.. been restructuring and taking a #WebDevelopment class. I'm 20% in... Currently on chapter 4 #JavaScript #RegularExpressions 6 more lessons, a quiz then I create a Math Game. Up next: Chapter 5 #JQuery. (at Chucks & Heels)