Get the App
SLTechnology News&Howtos  ›  Development  › 

How to create a RegExp object

Shulou Source: shulou.com Published: 2022-06-02 15:40:32 09月30日 Update

This article focuses on "how to create RegExp objects". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create RegExp objects.

How to create a RegExp object

In JavaScript, the first step in using a regular expression is to create a regular expression object, that is, a RegExp object.

The syntax for creating a RegExp object:

New RegExp (pattern,attributes)

Parameter description:

Parameter description

Required parameters for pattern. A string that specifies the pattern of the regular expression.

Attributes optional parameters. The match modifier can be "g", "I", "m", or a combination of them.

G: perform a global match (find all matches instead of stopping after the first match is found)

I: perform case-insensitive matching

M: perform multi-line matching.

For example, detect whether a string contains the letter "abc":

Var pattern=new RegExp ("abc")

Detect whether a string begins with the letter "abc":

Var pattern=new RegExp ("^ abc")

Detect whether a string is "abc":

Var pattern=new RegExp ("^ abc$")

The literal quantity of regular expression

JavaScript supports regular expressions literally, which makes the use of regular expressions more flexible.

Regular expression literal syntax:

/ pattern/attributes

The parameter description is the same as above.

For example, detect whether a string contains the letter "abc":

How to create a RegExp object

Var pattern=/abc/

Detect whether a string begins with the letter "abc":

Var pattern=/ ^ ABC /

Detect whether a string is "abc":

Var pattern=/ ^ ABC $/

Match modifier

The g modifier is used to perform a global match (to find all matches rather than stop after the first match is found). All major browsers support the g modifier.

For example, global matching of "is" in a string:

Var str= "Is this all there is?"

Var patt1=/is/g

The following tagged text shows where the string is matched:

Is this all there is?

The I modifier is used to perform case-insensitive matches. All major browsers support the I modifier.

For example, match the "itxueyuan" in a string regardless of case:

Var str= "Visit ItXueyuan"

Var patt1=/itxueyuan/i

The following tagged text shows where the string is matched:

Visit ItXueyuan

For example, make a case-insensitive global match of "itxueyuan" in a string:

Var str= "Itxueyuan is a website with various tutorials.Welcome to itxueyuan!"

Var patt1=/itxueyuan/ig

The following tagged text shows where the string is matched:

Itxueyuan is a website with various tutorials.Welcome to itxueyuan!

The m modifier is used to perform multiline matching. Before ECMAScript standardization, the m attribute was not supported. If pattern is a regular expression, not a string, you must omit this parameter.

At this point, I believe you have a deeper understanding of "how to create RegExp objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Characters strings objects regular expressions parameters detection global letters support location literal text tags mainstream content size uppercase lowercase beginning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info Docker Huawei Shulou Information