Basics
JS RegExp
Regular Expressions in JavaScript
JavaScript regular expressions use test and exec, with common patterns.
Introduction to Regular Expressions
Regular expressions (RegExp) in JavaScript are patterns used to match character combinations in strings. They can be used to perform all types of text search and text replace operations.
Creating a Regular Expression
There are two ways to create a RegExp object in JavaScript:
- Literal notation: Consists of a pattern enclosed between slashes.
- Constructor function: Uses the
RegExp()
constructor.
Using test() Method
The test()
method executes a search for a match between a RegExp and a specified string. It returns true
or false
.
Using exec() Method
The exec()
method executes a search for a match in a specified string. Unlike test()
, it returns a result array or null
if no match is found.
Common Patterns in Regular Expressions
Here are some common patterns used in regular expressions:
\d
: Matches any digit (equivalent to[0-9]
).\w
: Matches any alphanumeric character (equivalent to[a-zA-Z0-9_]
).\s
: Matches any whitespace character (spaces, tabs, etc.).^
: Matches the beginning of the string.$
: Matches the end of the string.*
: Matches 0 or more occurrences of the preceding character.+
: Matches 1 or more occurrences of the preceding character.
Basics
- Introduction
- Where To
- Output
- Syntax
- Comments
- Variables
- Scope
- Hoisting
- Errors
- Data Types
- Operators
- Ternary Operator
- Short-Circuit Evaluation
- If Else
- Switch
- Loops
- For...Of/For...In
- Functions
- this Keyword
- Objects
- Arrays
- Strings
- Template Literals
- Numbers
- Number Properties
- Dates
- Math
- Booleans
- Type Conversion
- Destructuring
- Spread/Rest
- RegExp
- Strict Mode
- Modules
- Security Basics
- Debugging
- Best Practices
- Mistakes
- Performance
- Reserved Words
- Sets
- Maps
- Bitwise
- Array Const
- Previous
- Spread/Rest
- Next
- Strict Mode