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

What are the RegExp and String knowledge points of javascript?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what are the RegExp and String knowledge points of javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

How to create RegExp (regular expressions)

You can create a RegExp in two ways, as follows:

Through /... . / to create a regular expression (Note: /... / there are no single or double quotation marks on both sides)

Create a regular expression through the RegExp constructor

In order to better describe the pattern, regular expressions provide three identities, namely: g/i/m

G: global match: matches in the entire string, rather than stopping after the first match

I: ignore case matching

M: apply special characters at the beginning and end of a line (^ and $, respectively) to each line in a multiline string

For more information, please refer to the following code for further understanding:

Var regx = new RegExp ('are','g'); var regx1 = / are/g; / / commonly used creation method

Main properties of RegExp instance

According to the constructor of RegExp, we can probably guess the main properties of RegExp. Just learn about the instance properties. One thing to note, however, is that these instance properties cannot be traversed through for in.

You can refer to the following code to deepen your understanding:

Var regx1 = / are/g; / / the common creation method console.log ("source:" + regx.source + "global:" + regx.global+ "ignoreCase:" + regx.ignoreCase + "multiline:" + regx.multiline); / / source:are global:true ignoreCase:false multiline:falsefor (var p in regx) {/ / will not enter the for loop if (regx.hasOwnProperty (p)) {console.log (regx [p]);}}

The main method of RegExp instance-test

According to this method, it is very simple, with only one parameter, which is often used to verify whether the input parameter matches the regular expression pattern, and returns true if it matches, false otherwise. You can refer to the following code to deepen your understanding:

Var regx1 = / are/g;var res = regx.test ('you are a good Boys'); console.log (res); / / truevar res1 = regx.test ('I am a good Boys'); console.log (res1); / / false

The main method of RegExp instance-exec

This method is a very common method and needs to be well understood. It receives only one parameter, that is, the string to match, but the return value is an array of arr, which stores information about the first match, including:

Input: the string to match, the input value of the exec method

Index: the position of the match in the string

Arr [0]: string for pattern matching

Arr [1]... Arr [n]: nth capture group string

Note when using this method: if the global flag g is not specified in the regular expression, the first match is always returned for each execution. If the global flag g is set and exec is called each time, the new match will continue to be found in the string.

You can refer to the following code to deepen your understanding:

Var regx = / fn: (\ w +)\ s+ln: (\ W +)\ s your fn:xiaoxin ln:tang right g; var s = "your fn:xiaoxin ln:tang right?"; var result = regx.exec (s); console.log (result.input); / / your fn:xiaoxin ln:tang right? Console.log (result.index); / 5 console.log (result [0]); / / fn:xiaoxin ln:tang console.log (result [1]); / / xiaoxin console.log (result [2]); / / tang console.log (result [3]); / / undefined prints undefined because there are only two capture groups

RegExp constructor properties

Function properties can be understood by referring to the static properties of classes in other programming languages (such as java). These properties are shared by all RegExp instances, that is, all RegExp can access and modify these properties. When an instance executes the test or exec method, the values of these properties will change accordingly.

With regard to these attributes, we can remember according to our own understanding:

Input: the string that requires pattern matching, the input parameter of the test or exec method. Parameter alias: $-

LastMatch: the last match. Parameter alias: $&

LeftContext: the string to the left of the match. Parameter alias: $`

RightContext: the string to the right of the match. Parameter alias: $'

1, 2, 2, 2, 3, 3, 5, 5, 5, 5, 4, 3. .: capture the string corresponding to the group.

Of course, these values can be calculated from the results returned by the exec executed by the RegExp instance, so why set these properties in the constructor RegExp? *

You can refer to the following code to deepen your understanding:

Var regx = / fn: (\ w +)\ s+ln: (\ w +)\ sUnix gX var s = "your fn:xiaoxin ln:tang right?"; var result = regx.exec (s); console.log (RegExp.input); / / your fn:xiaoxin ln:tang right?console.log (RegExp.lastMatch); / / fn:xiaoxin ln:tangconsole.log (RegExp.leftContext); / / yourconsole.log (RegExp.rightContext); / / right?console.log (RegExp.$1); / / xiaoxinconsole.log (RegExp.$2) / / tang

RegExp-metacharacter

Like regular expressions in other languages, there are some metacharacters in js regular expressions, which have special uses and meanings, so in the process of use, these characters need to be escaped by adding'\ 'before these characters. The metacharacters that JS regular expresses yes are:

([{\ ^ $|)? * +. ]}

RegExp-greedy matching and lazy matching

Greedy matching means that in the matching process of regular expressions, the default is to make the matching length as large as possible. In JS regular expressions, the lazy qualifier is'?', and add'?'to the pattern. Is required to be lazy to match. Refer to the following code for understanding:

Var s ='I am a good boy,you are also a good boy!'; var regx = / good.*boy/g; / / greedy matching console.log (regx.exec (s) [0]); / / good boy,you are also a good boyvar regx1 = / good.*?boy/g; / / lazy matching console.log (regx1.exec (s) [0]) / / this is the end of good boy's "what are the RegExp and String knowledge points of javascript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report