Regex Introduction

Regular expressions provide a powerful way to search and manipulate text. If you're familiar with SQL, you can think of regular expressions as being somewhat similar to SQL: you use SQL to find and update data inside a database, and you use regular expressions to find and update data inside a piece of text.

"regular expression" is often shorten and called as "regex" or "regexp".

A regular expression consists of:

  • A pattern you use to match text
  • Zero or more modifiers (also called flags) that provide more instructions on how the pattern should be applied

JavaScript provides the RegExp() constructor which allows you to create regular expression objects.

var re = new RegExp("j.*t");

There is also the more convenient regexp literal:

var re = /j.*t/;

In the example above, j.*t is the regular expression pattern. It means, "Match any string that starts with j, ends with t and has zero or more characters in between". The asterisk * means "zero or more of the preceding"; the dot (.) means "any character". The pattern needs to be placed in quotation marks when used in a RegExp() constructor.

Related Tutorial
Follow Us #
Contents +