In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to understand JavaScript regular expression, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
I. regular expression creation
1.var reg = / test/
2.var reg = new RegExp ('test')
3. Exec method of regular expression
Var reg = / test/
Var str = 'testrjf'
Var res = reg.exec (str)
/ / will match the test content in str. If there is a match, the matching content will be returned. No null will be returned.
Console.log (res); / / save test as an array
Second, regular expression pattern
Write a method here to facilitate later demonstration.
Function execReg (reg,str) {
Var result = reg.exec (str)
Console.log (result)
} 1. One piece, two tablets, three or four tablets, there are no {n}, {1,}, {1,}.
Reg = / c {1} /; / / matches a c
Str='cainiao'
ExecReg (reg,str); / / returns c
Reg = / c {2} /; / / matches two consecutive c, {n}
Str1='ccaniao'
Str2='cacniao'
ExecReg (reg,str1); / / returns cc
ExecReg (reg,str2); / / returns null. There are no two consecutive cc
Reg = / c {3pr 4} /; / / match 3 or 4 consecutive c
Str1='cccTest'
Str2='ccccTest'
ExecReg (reg,str1); / / returns ccc
ExecReg (reg,str2); / / returns cccc. (regular will match as many as possible)
Reg = / c {1,} /; / / matches more than one c. {2,} {3,}... The same principle
Str1='cccTest'
Str2='chjjk'
ExecReg (reg,str1); / / returns ccc
ExecReg (reg,str2); / / returns c
Reg='/c*/ is equivalent to reg='/c {0,} /
Reg='/c+/ is equivalent to reg='/c {1,} /
Reg='/c?/ is equivalent to reg='/c {0jue 1} /
As a hint here, the regular expression will match as many targets as possible, and if there are four, it will not return three.
2. / ^ begins and ends with $/
The beginning of ^
Reg = / ^ c _ bump / ^ means that only the beginning of the string is matched.
Str1=' vitamin c'
Str2='cainiao'
ExecReg (reg,str1); / / the result is null, because the string "forex analyst" does not begin with c
ExecReg (reg,str2); / / return c this time
End $
Reg = / characters at the end of the string matching the character of the string
Str1='cainiao'
Str2=' vitamin c'
ExecReg (reg,str1); / / returns null
ExecReg (reg,str2); / / returns c
3. Point "."
Reg = /. / /'.' Matches all characters in the string except the newline character\ n
Str1='cainiao'
Str2='bainiao'
ExecReg (reg,str1); / / regular match to the character c
ExecReg (reg,str2); / / regular match to the character b
Reg =. + /
Str1='blueidea- Classic Forum is good.'
Str2='bbs.blueidea.com'
Str3='\ ncainiao'
ExecReg (reg,str1)
/ / return 'blueidea- Classic Forum Hao _. All the characters are matched.
ExecReg (reg,str2); / / returns' bbs.blueidea.com'
ExecReg (reg,str3); / / returns null
4. Or "|"
Reg = / b | c /
Str1='blueidea'
Str2='cainiao'
ExecReg (reg,str1); / / returns b
ExecReg (reg,str2); / / returns c
Reg = / ^ b | c. Resume /
Str='cainiao'
ExecReg (reg,str); / / returns cainiao, which is understood as the beginning b | c and then. +
Reg = / ^ b | c. Resume /
Str='bbs.blueidea.com'
ExecReg (reg,str); / / returns b, which is seen here as the beginning of b or c +
5. Parenthesis
Reg = / ^ (b | c). + /
Str='bbs.blueidea.com'
ExecReg (reg,str); / / this is the beginning b | c, then. +
/ / there is an extra ", b" after the result, which is the match of b | c in ().
/ / what we write in parentheses inside a regular expression will be considered a sub-regular expression, and the matching results will be recorded for later use. Let's ignore this feature for the time being.
6. Character set [abc]
Reg = / ^ [abc] /; / / [abc] represents any character in an or b or c
Str='bbs.blueidea.com'
ExecReg (reg,str); / / returns b
Reg = / ^ [a-zA-Z] [a-zA-Z0-9] + /; / / first ^ [a-zA-Z], then [a-zA-Z0-9] +
Str='test'
ExecReg (reg,str); / / returns test
7. Anti-character set [^ abc]
Reg = / [^ abc] /
Str='blueidea'
ExecReg (reg,str); / / returns l (that is, the first b does not match)
That is, [^ 0-9] means non-numeric, and [^ amurz] represents non-lowercase letters.
8. Boundary and non-boundary
\ b the meaning of the boundary
Reg = /\ bc/;// indicates the c at the beginning or end of the string
Str1='cainiao'
Str2=' vitamin c'
ExecReg (reg,str1); / / returns c, the c of the left boundary
ExecReg (reg,str2); / / returns c, the c of the right boundary
\ B means non-boundary
Reg = /\ Bc/;// represents a string except for the c at the beginning (end)
Str='aicniao'
ExecReg (reg,str); / / returns c, the middle c
9. Digital and non-digital
\ d means a number.
Reg = /\ d /
Str='cainiao8'
ExecReg (reg,str); / / returns 8
\ d means non-numeric
Reg = /\ d /
Str='cainiao8'
ExecReg (reg,str); / / returns c
10. Blank
\ f match feed character,\ nmatch line feed character,\ r match carriage return,\ t match table character,\ v match vertical tab character.
\ s matches a single space, which is equivalent to [\ f\ n\ r\ t\ v].
Reg = /\ s.clients /
Str='This is a test String.'
ExecReg (reg,str); / / returns'is a test String.', matches the first space and all subsequent non-newline characters.
\ S represents a non-space character.
Reg = /\ slide /
Str='This is a test String.'
ExecReg (reg,str); / / This, when the first space is encountered, the regular stops matching
11. Word character
\ w represents a word character, which is equivalent to a character set [a-zA-Z0-9 _].
Reg = /\ walled /
Str='.blueidea'
ExecReg (reg,str); / / blueidea
\ W represents a non-word character, equivalent to [^ a-zA-Z0-9 _].
Reg = /\ Ware /
How about str=' in Chinese?'
ExecReg (reg,str); / / 'how about Chinese?'
twelve。 Reverse reference
Reg = / (\ w)\ 1 /
Str1='blueidea'
Str2='bblueidea'
ExecReg (reg,str1); / / returns null
ExecReg (reg,str2); / / bb
/ / the "\ 1" here is called a backreference, which represents what the word regular expression matches in the first parenthesis.
/ / in the above example, the first parenthesis (\ w) matches b, and "\ 1" also means b, so you can't find b in the rest of the string.
13. Do not record the matching results of sub-regular expressions
Using regularities such as (?: pattern) can avoid saving the matching results in parentheses.
Reg = / ^ (?: B | c). + /
Str='bbs.blueidea.com'
ExecReg (reg,str); / / returns bbs.blueidea.com
/ / the parenthesized subexpression mentioned earlier is not saved.
14. Forward pre-check
Form: (? = pattern), that is, the string to match must be followed by pattern!
Reg = / cainiao (? = 8) /; / / it must be followed by 8, but the result does not match 8
Str1='cainiao9'
Str2='cainiao8'
ExecReg (reg,str1); / / returns null
ExecReg (reg,str2); / / returns cainiao
Form: (?! pattern) and? = are just the opposite, requiring that the string cannot be followed by a pattern.
Reg = / cainiao (?! 8) /
Str1='cainiao9'
Str2='cainiao8'
ExecReg (reg,str1); / / returns cainiao
ExecReg (reg,str2); / / returns null
15. Match metacharacter
*, +,? They all have a special meaning in regular expressions, and characters with special functions are called metacharacters. Then we want to play the match with 'crested'. What about this string?
Reg = / c\? /
Str='c?'
ExecReg (reg,str); / / returns' clocked'
Regular expression modifiers
1.I: performs a case-insensitive match.
Var reg = / b /
Var str = 'BBS'
ExecReg (reg,str); / / returns null
Var reg = / bgambi
Var str = 'BBS'
ExecReg (reg,str); / / B
2.g: perform a global match (find all matches instead of stopping after the first match is found)
It will be explained later.
3.m: perform multi-line matching.
Reg = / ^ b /
Str = 'test\ nbbs';// has line feeds
ExecReg (reg,str); / / null
Reg = / ^ baggage
Str = 'test\ nbbs'
ExecReg (reg,str); / / returns b, starting with the second line b after the line breaks
4. Regular expression method
1.exec method
What the exec method returns is not a matching result string, but an object, which can be confirmed by simply modifying the execReg function to do an experiment.
Function execReg (reg,str) {
Var result = reg.exec (str)
Console.log (typeof result)
Reg = / b /
Str='bbs.bblueidea.com'
ExecReg (reg,str)
/ / shows that the type of result is object. And it's an array-like object.
Where attribute: index input 0. Where index is the index that represents the match in the original string; input is the string that represents the input; and 0 indicates that there is only one matching result, which can be referenced by subscript 0, and this number may change. We can know the total number of matches by returning the length property of the value.
Then modify the execReg function
Function execReg (reg,str) {
Var result = reg.exec (str)
Console.log ('index:'+result.index+'\ n` result\ n')
For (iSuppli)
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.