Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand JS regular RegExp object

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand JS regular RegExp objects". In daily operation, I believe many people have doubts about how to understand JS regular RegExp objects. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to understand JS regular RegExp objects". Next, please follow the editor to study!

1. RegExp object

A regular expression is an object that describes a character pattern.

Regular expressions are used for string pattern matching and retrieval substitution, and are a powerful tool for performing pattern matching on strings.

Reference textbook: w3cschool | JavaScript RegExp object

2. Syntax 2.1 definition

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

/ * for example, the definition of the following two ways is equivalent * / / Constructor mode const reg = new RegExp ("\\ w+"); / / literal mode const reg = /\ wattabble 2.2 modifier

Used to perform case-sensitive and global matching

The modifier describes that I performs a case-insensitive match. G performs a global match (looking for all matches instead of stopping after the first match is found). M performs multi-line matching. / * Chestnut * / const reg = /\ w/gi2.3 square brackets

Used to find characters in a range:

The expression describes [abc] to find any character between square brackets. [^ ABC] look for any characters that are not between square brackets. [0-9] look for any number from 0 to 9. [aMuz] finds any character from lowercase a to lowercase z. [Amurz] looks for any character from uppercase A to uppercase Z. [Amurz] looks for any character from uppercase A to lowercase z. [adgk] finds any character within a given collection. [^ adgk] finds any character outside a given collection. (red | blue | green) find any specified options. / * Chestnut * / const reg = / [0-9] / g2.4 metacharacter

Are characters with a special meaning:

Metacharacter description. Look for a single character, except for line breaks and line Terminators. \ w look for word characters. \ W look for non-word characters. \ d look for numbers. \ d look for non-numeric characters. \ s look for white space characters. \ s looks for non-white space characters. \ b match word boundaries. \ B matches non-word boundaries. \ 0 find NUL characters. \ nLook for newline characters. \ f find the feed character. \ r look for carriage returns. \ t find tabs. \ v find vertical tabs. \ xxx looks for the characters specified by the octal number xxx. \ xdd looks for characters specified in the hexadecimal number dd. \ uxxxx looks for Unicode characters specified in the hexadecimal number xxxx. / * Chestnut * / const reg = /\ dCompact g / / match the number 2.5quantifier

Are characters with a special meaning:

The quantifier description n + matches any string that contains at least one n. N * matches any string that contains zero or more ns. N? Matches any string that contains zero or one n. N {X} matches a string containing a sequence of X n. N {XQuery Y} matches a string containing a sequence of X to Y n. N {X,} matches a string containing a sequence of at least X n. N $matches any string that ends with n. ^ n matches any string that starts with n. ? = n matches any string followed by the specified string n. ?! n matches any string that is not followed by the specified string n. / * Chestnut * / const reg = /\ d match at least one number 2.6 method

Are characters with a special meaning:

Method describes how FFIEcompile compiles regular expressions. 14exec retrieves the value specified in the string. Returns the value found and determines its location. 14test retrieves the value specified in the string. Returns true or false. The 14 method describes how FFIEsearch retrieves values that match regular expressions. 14match found a match for one or more regular expressions. 14replace replaces the substring that matches the regular expression. 14split splits a string into an array of strings. 14 Hello * Chestnut * / var patt = / Hello/gvar result = patt.test (str) / / find the Hello string-> true here, the study on "how to understand JS regular RegExp objects" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report