Regular Expression -Tutorial

A regular expression (Regular Expression, abbreviated as Regex or RegExp) is a pattern used to match character combinations in strings.

A regular expression is 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.

Introductory video:Regular Expression Introductory Tutorial。


What can regular expressions do?

  • Find:Find content with specific patterns in text
  • Replace:Replace text matching a certain 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 from the string str:

Example

Extract the numeric part from string str (match once):

var str = "abc123def"; var patt1 = /[0-9]+/; document.write(str.match(patt1));

The following marked text is the expression that obtained the match:

123

Try it »

Testing Tools

Modifiers:

[0-9]+

Matched text:

abc123def

Patterns of Regular Expressions

Patterns of regular expressions can include the following:

  • Literal characters: such as letters, numbers, spaces, etc., can directly match themselves.

  • Special characters: such as dot., asterisk*, plus sign+, question mark?etc. They have special meanings and functions.

  • Character classes: a set of characters enclosed by square brackets[ ], used to match any single character within the square brackets.

  • Metacharacters: such as\d、\w、\setc., used to match specific types of characters, such as digits, letters, whitespace characters, etc.

  • Quantifiers: such as{n}、{n,}、{n,m}etc., used to specify the number or range of matches.

  • Boundary symbols: such as^、$、\b、\Betc., used to match the beginning, end, or word boundary positions of a string.

Content List

Other Related Tools

Other Extensions