Regular Expressions (regex) are character-matching patterns that are used to find and/or replace character patterns in strings.
With Regular Expressions, you can:
- Test for a pattern within a string. For example, you can test an input string to see if a telephone number pattern or a credit card number pattern occurs within the string.
- Replace text. You can use a Regular Expression to identify specific text in a string and either remove it completely or replace it with other text.
- Extract a substring from a string based upon a pattern match.
The syntax of Regular Expressions is not easy to understand. If you have problems understanding it, you are advised to check the examples, try to modify them and test what the differences are. Regular Expressions were first popularized by the Perl scripting language, and most implementations are based on Perl. For further reading about Regular Expressions, you may search for Perl documentation in addition to JScript . NET or JavaScript documentation.
Note: For additional information on JavaScript's implementation of regex, see Mozilla's developer guide on Regular Expressions.
Regular Expressions are implemented as Regular Expression objects, and are created as follows:
var re = /pattern/{flags};(similar to Regular Expressions in Perl) or
var re = new RegExp("pattern",{"flags"});(similar to normal syntax for instantiating an object in JScript .NET)
pattern is the pattern to be matched and the optional flags is a string containing i, and/or m. The i stands for ignore case and the m for multi-line search.
Important: When defining a Regular Expression with the syntax /pattern/flags, do not use quotation marks around the strings. However when using the other notation you must use them.
var re = new RegExp("Forsta","i");
is the same as
var re = /Forsta/i;
This matches all occurrences of "Forsta".