In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the use of regular expressions in javascript. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.
In JavaScript, regular expressions are commonly used for two string methods: search () and replace ().
The search () method is used to retrieve a specified substring in a string, or to retrieve a substring that matches a regular expression, and returns the starting position of the substring.
The replace () method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression.
1. Matching pattern
The regular expression finds the part of the string that matches the pattern
In JavaScript, they are between forward slashes / or using new RegExp ()
For example:
Var str = "Visit Jb51.net!"
Var n = str.search (/ jb51.net/i)
Example 2
Var reg=new RegExp ("end of antiquity", "g"); / / create regular RegExp objects var stringObj= "end ancient people's Republic, end ancient people"; var newstr=stringObj.replace (reg, "China"); alert (newstr)
Example 3
Function dotag () {the first way to write RegExpdocument.form1.tag.value = trim (new RegExp (',', "gm"),','); the second way to use / / document.form1.tag.value = trim (document.form1.tag.value.replace (/, / gm, ",");}
Is it better to look better in the second? if you encounter the first kind of\ w, you have to add one more\ w, so in general, we use /.
For more information, please refer to this article: introduction to the use of JavaScript replace new RegExp
Then used for match, test or replace
You can define regular expressions in advance or directly when a method is called
two。 Match multiple
Match a single character at a time
Or place multiple characters in square brackets [] to capture any matching characters
Use a hyphen to capture a series of characters.
Give an example
[0-9] represents the number of 0, 1, and 2. 9.
[amurz] stands for 26 lowercase letters
[Amurz] stands for 26 capital letters
3. Optional flag
Add an optional flag to the end of the regular expression to modify how the matcher works.
In JavaScript, these flags are:
I = case-insensitive m = multi-line match g = global match (find all, not one)
4. Start and end
Use the caret ^ at the beginning for "string start"
Use the dollar sign $at the end for "end of string"
Start grouping matches together to match longer strings
In form validation, you need to add $after ^.
For more information, please refer to this article: https://www.jb51.net/article/118265.htm
5. Wildcard character
Use wildcards and special escape characters to match larger classes of characters
. = any character except newline character
\ d = number\ D = not a number
\ s = spaces\ S = any non-blank space
\ nNew line\ w non-empty letter
6. Specific quantity
Match only a specific number of matching characters or groups with quantifiers
= zero or more = there is another one? = 0 or 1 {3} = exactly 3 times {2pm 4} = double, triple or quadruple {2,} = two or more times
7. Parenthesis matching group
Use parens () to capture a group
Match returns a full match plus a group, unless you use the g flag
Use the pipe operator | inside parens () to specify the matching content of this group
| = or
8. Backslash
To match special characters, use a backslash\
The special character in the JS regular expression is: ^ $\. * +? () [] {} |
Therefore, to match an asterisk, you can use:
\ *
It's not just *
9. ^ in square brackets
To match a character but to match a character, use the caret ^ in square brackets
This means that ^ has two meanings and may cause confusion.
When it precedes a regular expression, it means "the beginning of a string", while when used in square brackets, it means "not this character".
These are the ways in which regular expressions are used in javascript shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.