Regular Expressions - Tutorial

Regular Expression (abbreviated as Regex or RegExp) is a pattern used to match character combinations in strings.
Regular expressions are a tool used for pattern matching and searching text.
Regular expressions provide a flexible and powerful way to find, replace, validate, and extract text data.
Regular expressions can be applied in various programming languages and text processing tools, such as JavaScript, Python, Java, Perl, etc.
Getting started video:Regular Expressions Beginner Tutorial.
What can regular expressions do?
- **Find:** locate content matching a specific pattern in text
- **Replace:** substitute text that matches a pattern with other content
- **Validate:** check whether input data conforms to the expected format
- **Extract:** extract needed information from complex text
Example
The following example finds numbers in the string str:
Example
Extract the numeric portion from the string str (match once):
var
str
=
"
abc123def
"
;
var
patt1
= /
[
0
9
]
+/;
document
.
write
(
str
.
match
(
patt1
)
)
;
The text marked below is the expression that obtains the match:
123
Try it »
Testing Tool
Modifiers:
[0-9]+
Match text:
abc123def
Regular expression patterns
A regular expression pattern can include the following:
-
Literal characters: for example letters, digits, spaces, etc., can directly match themselves.
-
Special characters: for example the dot
., asterisk*, plus sign+, question mark?, etc. They have special meanings and functions. -
Character classes: using square brackets
[ ]to enclose a set of characters, used to match any single character inside the square brackets -
Metacharacters: for example
\d,\w,\s, etc. They are used to match specific types of characters, such as digits, letters, and whitespace characters. -
Quantifiers: for example
{n},{n,},{n,m}, etc. They are used to specify the number or range of matches. -
Boundary symbols: for example
^,$,\b,\B, etc. They are used to match the beginning, end, or word boundary positions of a string.
Table of contents
- Regular Expression - Introduction
- Regular Expression - Syntax
- Regular Expression - Metacharacters
- Regular Expression - Operator Precedence
- Regular Expression - Matching Rules
- Regular Expression - Examples
- Regular Expressions - Online Tools
- Regular Expressions - Visualization Tool