Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript RegExp Object

RegExp: a regular expression (regular expression) shorthand.


Complete RegExp Object Reference

Please see our JavaScript RegExp object reference manual , which provides all of the properties and methods can be used in conjunction with the string object.

This manual contains a detailed description and examples on the usage of each of the properties and methods.


What is RegExp?

A regular expression describes a pattern of characters of the object.

When you retrieve a text, you can use a model to describe the content to be retrieved. RegExp is this mode.

Simple pattern can be a single character.

More complex models include more characters can be used to parse, format check, replace, and so on.

You can specify a search string position, and the type of characters you want to retrieve, and so on.

grammar

var patt=new RegExp(pattern,modifiers);

或更简单的方法

var patt=/pattern/modifiers;
  • Model describes a model expression.
  • Modifiers (modifiers) describes whether the retrieval is global, case-sensitive and so on.

Note: When using a constructor to create a regular objects, you need regular character escape rules (preceded by a backslash \). For example, the following are equivalent:

var re = new RegExp("\\w+");
var re = /\w+/;

RegExp Modifiers

Modifier is used to perform a case-insensitive and full-text search.

i - modifier is used to perform a case-insensitive match.

g - modifier is used to perform full-text searches (rather than finding the first stop to find, but to find all matches).

Example 1

In a case-insensitive string find "W3CSchool"

var str="Visit W3CSchool";
var patt1=/w3cschool/i;

The following text is marked matching expression obtained:

Visit W3CSchool

try it"

Example 2

Full Text Search "is"

var str="Is this all there is?";
var patt1=/is/g;

The following text is marked matching expression obtained:

Is th is all there is ?

try it"

Example 3

Full Text Search and case-insensitive search "is"

var str="Is this all there is?";
var patt1=/is/gi;

The following text is marked matching expression obtained:

Is th is all there is ?

try it"


test ()

Value method specified search string test (), based on the results and returns true or false.

The following example is a search string from the character "e":

Examples

var patt1 = new RegExp ( "e");
document.write (patt1.test ( "The best things in life are free"));

Because of the letter "e" in the string, the output of the code above will be:

true

try it"

When using a constructor to create a regular objects, you need regular character escape rules (preceded by a backslash \)

Examples

var re = new RegExp ( "\\ w +");

try it"


exec ()

() Method to retrieve the specified value exec string. The return value is the value to be found. If no match is found, it returns null.

The following example is a search string from the character "e":

Example 1

var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));

Because of the letter "e" in the string, the output of the code above will be:

e

try it"