The best JavaScript RegExp Object Tutorial In 2024, In this tutorial you can learn Complete RegExp Object Reference,What is RegExp?,grammar,RegExp Modifiers,Example 1,Example 2,Example 3,test (),Examples,Examples,exec (),Example 1,

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


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 ?


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 ?



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

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 +");



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

JavaScript RegExp Object
10/30