Java Script regular expression entry (2)

xiaoxiao2021-03-06  48

If you have already seen the first article, then you have understood what is regular expression, then we will learn more in-depth to learn the regular expression in Java Script.

Third, Java Script Regular Expression Basic Syntax: In order to learn these things, let us introduce from the following aspects: 1, match mode: The matching mode should be considered the essence of regular expressions. He expressed your request (don't forget your evaluation criteria for beautiful MM, haha). If you want to verify that a string is in a certain format, then the matching mode expresses the string you want to verify that you need to match the standard. If you want to verify a sub-string containing some format in a period, the matching mode represents the format of the string you are looking for. If you want to replace the character string in a certain format from a piece of text string, the matching mode represents the standard that will meet the string that will be replaced ... (Trump ). So how do I define a pattern in Java Script? There are two ways to define the model in Java Script, but now I only tell you the first most common way, another waiting for me to sleep, learn to tell you, huh, huh. Method of defining a matching mode: var regobj = /.../[flags]; then, now regoBJ is a regular expression object. Sign! With / start, then / end, there is no single quotes and double quotes on both sides. What is omitted in the middle? Tell you, that is the essence: mode. Take a look at Microsoft's definition: re = / pattern / [flags]

RE

required. The variable name that will be assigned to the regular expression mode.

Pattern

required. The regular expression pattern to be used.

Flags

Optional. The logo can be used in combination, available in: g (all Pattern) i (ignore the case) M (multiple lines) M (multi-line lookup) You see, he uses var and end characters ";" I think using VAR and ";" Will make your program clearer. Of course, it is not legal, but it is not necessarily good. Just like you find MM, height: match, PP: conformity. . . It is in line with it, but it doesn't feel it. . Haha. . Come on. . Luo Yu hasn't said that I haven't said it for a long time. I am sleepy. . Now there are several small examples, and there are several common patterns. If you say more, you will be dizzy, I will not be clear. When you have finished using the regular expression how to use the mode, you should be interested in concern about these Chen Valley. It's better and more gossip. For example: the first: var numberpattern = / ^ / d * $ /; // Verify a string must be all numbers. Description: ^ represents the beginning of a string; $ represents an end of a string; / d is used to match a Arabic number; * is to match / D multiple times. That is, it is to end from a string to the end, and it must be zero or more numbers. Of course, you can also write this / ^ [0-9] * $ /, if you don't understand, it may be that you are more sleepy, sleep will come. Look at a most stupid way: / ^ [0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9] * $ /. "|" Use here is stupid, but it is quite smart to use other places, do not believe in the next: The second: var monthpattern = / ^ [0 | 1]? / D $ / understand ? A number of numbers in one month must be a number of 1-12 species, of course, you can write: 09. Here "|" should be quite smart. ? : The representative matches 0 or 1 time. No more example, I will give me a common library to here after I have finished writing this article. I will use it.

2, regular expression object: You have seen the first way to define the regular expression, now I will tell you the second definition method, see: var regobj = new regexp ("pattern" [, "flags"] ); // pay attention to double quotation marks and cannot be lost. The meaning of the parameters is unhappy. The regoBj defined here is exactly the same as the one defined in the first way. Since it is an object, we should study its properties and methods. First see the property: Haha, only two, what is it: 1) LastIndex: According to Microsoft, he should only belong to the regexp object, may be the author of the document Wrong. 2) Source: Returns the copy of the text of the regular expression mode. Read only. As so much, look at the method: 1) Compile (pattern [, flags]): This method converts Pattern to internal formats, thereby performing faster. For example, this makes it more efficiently using regular expressions more efficiently in the cycle. When the same expression is repeated, the compiled regular expression makes it accelerated. However, if the regular expression changes, this compilation is no longer. (This speed is increased is that you can't see it.). I simply tell you, let this method forget!

2) Test (STR): You have already seen, return a boolean value, indicating whether there is a mode in the string of the lookup. You will use it when you perform mode verification. 3) EXEC (STR): Let Microsoft tell you about it, guarantee you to speak you: Function: Run the lookup in the string with the regular expression mode and return an array containing the lookup results. Note: If the Exec method does not find a match, it returns NULL. If it finds a match, the exec method returns an array and updates the properties of the global REGEXP object to reflect the results. The 0 element of the array contains a complete match, and the first to n elements are included in any one of the matching of the match. This is equivalent to a MATCH method that does not set global flags (G). If a global flag is set for regular expressions, Exec starts looking for locations indicated by the value of LastIndex. If the global flag is not set, Exec ignores the value of LastIndex, starts from the starting position of the string. The array returned by the exec method has three properties, namely INPUT, INDEX, and LastIndex. The Input property contains the entire string of strings. The index property contains the position of the sub-string that is matched throughout the string. The LastIndex property contains the next location of the last character in the match. Example: Function Regexptest () {var ver = number (Scriptenginemajorversion () ". Scriptengineminorversion ()) IF (Ver> = 5.5) {// Test the version of JScript. Var src = "The rain in spain falls mainly in the place."; VAR RE = // w / g; // Create a regular expression mode. Var Arr; While ((arr = re.exec (src))! = null) Document.write (Arr.index "-" arr.lastindex "/ t" arr);} else {Alert ("please Update version of JScript "); I was stunned. As for why we don't say, first look at the results: 0-3 the4-8 rain9-11 in12-17 spain18-23 Falls24-30 mainly31-33 in34-37 the38-43 plain If you are also dizzy, then the end I will diagnose you diagnose: 1) I didn't understand what he called "array" mean? 2) I don't know what the logo g is mean? 3) I don't know what the markers / w and mean in the mode? 4) Wait, etc.. . . (Of course, the most important thing is that Microsoft is unconscious!) Let me explain to you again, let you fall asleep from fainting! 3, regexp object: 4, String object supports the regular expression of 5, pattern matching syntax rules: four, in-depth understanding of the syntax:

V. Senior Application of Regular Expression:

Sixth, the order: the history of regular expressions

转载请注明原文地址:https://www.9cbs.com/read-82539.html

New Post(0)