Regular Expressions -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 Expressions Beginner Tutorial。


What Can Regular Expressions Do?

  • Find:Find content matching a specific pattern in text
  • Replace:Replace text that matches a certain pattern with other content
  • Validate:Check whether input data conforms to the expected format
  • Extract:Extract the required information from complex text

Examples

The following example finds digits from the string str:

Examples

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

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

The marked text below is the obtained matching expression:

123

Try it Yourself »

Testing Tool

Modifiers:

[0-9]+

Text to match:

abc123def

Regular Expression Patterns

Regular expression patterns can include the following:

  • Literal characters: for example, letters, digits, spaces, etc., which can directly match themselves.

  • Special characters: for example, the dot., asterisk*, plus sign+, question mark?etc., which have special meanings and functions.

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

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

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

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

Content List

Other Related Tools

Other Extensions