Regular expression
Regular expressions are used to define rules for strings
A regular expression is used to check whether a string conforms to a rule
Gets Extracts the content of the string that matches the rule
Regular expression method
test()
- You can use this method to check if a string conforms to the rule when the re gets larger
- Returns true if it does, false otherwise
Checks if the string contains a
A returns true, no false, strictly case sensitive
You can pass a matching pattern as a second argument in the constructor
- I Ignore case
- G Global matching mode
Regular expression two methods
Said a | b/a or b
/[ab]/ Checks a or B
String and re – related methods
splice()
-
You can split a string into an array
-
The method can pass a regular expression as an argument, and the method will split the string based on the regular expression
-
This method will split everything even if it does not specify a global match
search()
- You can search for the specified content in a string
- The first occurrence of the index is returned if the specified content is found, or -1 if not
- He can take a regular expression as an argument and then retrieve the string based on the regular expression
- Search () will only find the index position of the first one, and setting the global variable will not help
match()
- You can extract qualified content from a string based on a regular expression
- By default, our match will only find the first content that matches the requirements, and then stop and retrieve it. We can set the regular expression to global matching mode, which will match all the content
- Multiple matching patterns can be set for a regular expression in any order
- Match () encapsulates the matched contents into an array and returns only one result
replace()
- You can replace the specified content in the string with the new content
- Parameters:
- 1. The content to be replaced can take a regular expression as a parameter
- 2. If the new content is set to empty, it will be deleted
- The default value replaces the first one, and you can change all of them with global variables using regular expressions
quantifiers
- Quantifiers allow you to set the number of occurrences of a content
- A quantifier is a function of the content in front of it
- {n} occurs exactly n times
- {m,n} occurs m to n times
- {m,} occurs more than m times
- /ab+c/ represents at least one b, equivalent to {1,}
- /ab*c/ means zero or more, the same as {0,}, whatever
- /ab? C/stands for 0 or 1, equivalent to {0,1}
- ^ indicates the beginning
- $indicates the end
- If you use ^ $in a regular expression, the string must conform to the regular expression
Beginning end instance
- /^a/ Starts with a
- /a$/ ends with an A
- /^a$/ returns true if the string begins and ends with the same A
- ^ a | a $/ said to start or end with a or a
- /^a.*a$/ starts with a and ends with a
Regular exercises determine the phone number
Check and \
In console, \ also means an escape character, so \ means that \ will print out with only one \
Pay attention to
When you use the constructor, since its argument is a string and \ is an escape character in the string, you need to use \ instead
Escape character
- \w Any alphanumeric underscore is equivalent to [A-z 0-9 _]
- \W = [^ a-z 0-9 _] except alphanumeric underscore
- \d Any digit [0-9]
- \D Except number [^0-9]
- \ s Spaces
- \S Except Spaces
- \b Word boundary
- \B Except for word boundaries
Understand word boundaries
For example, looking for child will return true if the string contains children, but we want to look for a separate child word, so use \b to check for Spaces before and after the child
When tested with /\bchild\b/, the input string returns true as a separate word even if there is no space following the right-most child word, so everything is ok