Intelligently generate and explain regular expressions, supporting various common pattern matching
Convert your text instructions into formulas or input a formula to have it explained.
Edit Excel online by chatting with AI
Convert your text instructions into SQL queries - powered by AI.
Generate Excel VBA (Visual Basic for Applications) code to automate tasks and create custom solutions within Microsoft Excel.
Upload your Excel file and generate beautiful charts with our AI-powered chart generator.
Convert your text into beautiful mind maps with our AI-powered mind map generator. Edit and customize your mind maps easily.
Use AI to intelligently generate and explain regular expressions, supporting various text pattern matching and data validation.
# JavaScript Regular Expressions Tutorial (Complete Syntax + Common Examples)
> This article is a complete tutorial for JavaScript Regular Expressions (RegExp), from beginner to advanced. It includes syntax rules, common metacharacters, flags, assertions, groups, greedy vs. lazy matching, and is applicable to front-end development, web scraping, form validation, and other scenarios. It is suitable for beginners and advanced users to learn and reference.
## Basic Regex Syntax
### Two Ways to Create a Regular Expression
```js
const regex1 = /abc/; // Literal way
const regex2 = new RegExp('abc'); // Constructor way
Flag | Description |
---|---|
g |
Global match |
i |
Case-insensitive match |
m |
Multiline match |
s |
Allows . to match newline (ES2018) |
u |
Supports Unicode (ES6) |
y |
Sticky matching (ES6) |
Example:
const re = /hello/gi;
const str = 'Hello hello';
console.log(str.match(re)); // ["Hello", "hello"]
Character | Meaning |
---|---|
. |
Matches any character (except newline) |
\d |
Matches a digit [0-9] |
\D |
Matches a non-digit character |
\w |
Matches a word character (alphanumeric + underscore) |
\W |
Matches a non-word character |
\s |
Matches a whitespace character |
\S |
Matches a non-whitespace character |
\b |
Matches a word boundary |
\B |
Matches a non-word boundary |
\\ |
Matches a backslash |
/[abc]/ // Matches 'a' or 'b' or 'c'
/[a-z]/ // Matches lowercase letters
/[A-Z]/ // Matches uppercase letters
/[0-9]/ // Matches digits
/[^abc]/ // Matches any character except a, b, or c
Quantifier | Meaning |
---|---|
* |
Matches the preceding item 0 or more times |
+ |
Matches the preceding item 1 or more times |
? |
Matches the preceding item 0 or 1 time |
{n} |
Matches exactly n times |
{n,} |
Matches at least n times |
{n,m} |
Matches at least n times, at most m times |
Example:
/d{4}/.test("2025"); // true
/ab{2,4}c/.test("abbbc"); // true
/(abc)/ // Capturing group
/(abc){2}/ // Capturing group repeated
/(?:abc)/ // Non-capturing group
/(a)(b)(c)/ // Multiple capturing groups
Extracting matches:
const match = /(hello) (\w+)/.exec("hello world");
console.log(match[1]); // hello
console.log(match[2]); // world
/\d+(?=px)/ // Matches digits followed by px
/\d+(?!px)/ // Matches digits not followed by px
/(?<=\$)\d+/ // Matches digits preceded by $
/(?<!$)\d+/ // Matches digits not preceded by $
Expression | Matching Behavior |
---|---|
.* |
Greedy (as much as possible) |
.*? |
Lazy (as little as possible) |
Example:
const str = '<p>text</p><p>more</p>';
const greedy = /<p>.*<\/p>/;
const lazy = /<p>.*?<\/p>/g;
console.log(str.match(greedy)); // ['<p>text</p><p>more</p>']
console.log(str.match(lazy)); // ['<p>text</p>', '<p>more</p>']
/^[\w.-]+@[a-zA-Z\d.-]+\.[a-zA-Z]{2,}$/.test("[email protected]");
/^1[3-9]\d{9}$/.test("13888888888");
const cleaned = str.replace(/\s+/g, '');
const domain = url.match(/https?:\/\/([^\/]+)/)[1];
Method | Description |
---|---|
test(str) |
Tests if string matches |
exec(str) |
Returns match result object (with groups) |
str.match(re) |
Returns all matches (with global flag) |
str.replace() |
Replaces matched content |
str.split(re) |
Splits string using regex |
str.search(re) |
Returns the index of the first match |
This tutorial provides a systematic overview of JavaScript Regular Expression syntax and practical application examples, suitable for front-end development, back-end development, text processing, and various other use cases. Mastering regular expressions can greatly improve string processing efficiency.