This article has participated in the Denver Nuggets Creators Camp 3 “More Productive writing” track, see details: Digg project | creators Camp 3 ongoing, “write” personal impact
preface
Regular expressions are a powerful tool for matching special characters and special combinations of strings. Today we’ll take a look at the basics to get started and review
Create a way
- literal
Format: / pattern/attributes
var reg = /abc/g // indicates that the ABC string is matched globally
Copy the code
- Created by new RegExp
Format: New RegExp(Pattern, Attributes)
var reg = new RegExp('abc'.'g')
Copy the code
Description:
- Pattern Indicates the pattern of the regular expression, that is, the rule to be matched
- Attributes is an optional parameter that includes g, I, and m, representing global matching, case – and multi-line matching, respectively. The defaults are non-global, case-insensitive, and single-line matching
- The return value is a RegExp object with the specified mode flag, typeof REg as Object
- SyntaxError is reported when Pattern is an invalid regular or attributes contains characters other than GIm
Let’s look at some specific rules
Some of the rules
- [] is used to find a character in a range
- [ABC] Searches for any character in ABC
- [^ ABC] finds any character that is not a, b, or B; ^ in [] indicates not
- [0-9] looks for any number from 0 to 9, and – means to or to
- [a-z] searches for characters from A to Z
- [a-z] searches for characters from A to Z
- [a-z] searches for characters from A to Z
- A metacharacter represents a character ‘that has a special meaning
- . Represents any character except newline; available
\.
Represents the single character “. - \w represents word characters
- \W represents a non-word character
- \d stands for numeric character
- \D stands for non-numeric characters
- \s means to find whitespace characters
- \S means to find non-whitespace characters
- \ n line
- \ r a carriage return
- \ t tabs
- \uxxxx indicates the Unicode character corresponding to the hexadecimal number XXXX
- () represents a set of regular rules that can be decorated as a whole or used for subsequent calls
The portion of a regular expression enclosed in parentheses can be treated as a “group.” This group can be modified as a whole, or the matching results of this part of the re can be obtained separately in the subsequent processing. The parentheses treat it as a group and note the result of the match. Regular expression \n represents the result of the “NTH group”. Since there may be more than one parenthesized group in a regular expression, \1 \2 \3 and so on are used to represent the previous group 1, group 2, and group 3 respectively
- Quantifiers represent the number of characters that occur
+ means one to more
* indicates zero to multiple numbers
? Indicates that 0 or 1 is saved
{x} indicates the occurrence of x strings
{x,y} represents strings from X to y
{x,} represents x to infinity strings
What ending string does $represent, such as /n$/ for a string ending in n
^ for a string that starts with what, such as /^n/ for a string that starts with the letter n
? = represents a string followed by a string, such as /? =n/ represents a string followed by the letter n
? ! A string that is not immediately followed by a string, such as /? ! N/matches any string not immediately followed by n
| said or, for example (x | y) to match x or y
- Common methods for re objects
- Compile () compiles and changes the re
- Test () is used to check whether a string contains a string represented by a regular expression. Returns true or false
- Exec () checks if a string contains the string represented by the regular expression, returning an object that records the value found and the index index of the position first encountered
6. Common methods for wrapping string like objects (re related)
- Use the re in replace()
Represents a substring in the replacement string that is matched by the re
var str = "Visit Microsoft!" ; var res = str.replace(/microsoft/i, "W3School"); // The result of res will be: Visit W3School!Copy the code
- Search finds the index position in the string of a substring that matches the re, returns -1 if no match is found
- Match is used to return an array of substrings that match the re