In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use JavaScript in regular expressions, which is very detailed and has a certain reference value. Friends who are interested must read it!
Look at the application of regular expressions in JavaScript. Before you start, take a look at several properties of the RegExp instance.
The RegExp instance object has five properties
1.global: whether to search globally. Default is false.
2.ignoreCase: whether it is case sensitive. Default is false.
3.multiline: multi-line search. Default is false.
4.lastIndex: is the next position of the last character in the content that the current expression pattern matches for the first time, and the value of the lastIndex attribute changes each time the regular expression matches successfully.
5.source: text string of a regular expression
In addition to compiling regular expressions to an internal format to execute the compile () method faster, there are two other common methods for objects
RegObj.test (strObj)
Method is used to test whether a regular expression pattern exists in a string parameter, and returns true if it exists, or false otherwise
The code is as follows:
Var reg=/\ d +\.\ d {1Magne2} $/ gsetreg.test ('123.45'); / / truereg.test (' 0.2'); / / truereg.test ('a.34'); / / falsereg.test (' 34.5678'); / / false
RegObj.exec (strObj)
Method is used for regular expression patterns to run a lookup in a string, and if exec () finds matching text, it returns an array of results. Otherwise, return null. In addition to array elements and length attributes, the exec () method returns two attributes. The index attribute declares the position of the first character that matches the text. The input attribute holds the retrieved string string.
When you call exec () of a non-global RegExp object, the 0th element of the returned array is the text that matches the regular expression, the first element is the text that matches the first subexpression of RegExpObject, if any, the second element is the text that matches the second subexpression of the RegExp object, and so on.
When you call exec () of the global RegExp object, it starts retrieving the string string at the character specified by the lastIndex property of the RegExp instance. When exec () finds the text that matches the expression, after matching, it sets the lastIndex property of the RegExp instance to the next position of the last character of the matching text. You can iterate through all matching text in a string by repeatedly calling the exec () method. When exec () can no longer find matching text, it returns null and resets the lastIndex property to 0.
The code is as follows:
Var reg=/\ dAccordia r=reg.exec ('a1b2c3'); console.log (reg.lastIndex); / / 2r=reg.exec (' a1b2c3'); console.log (reg.lastIndex); / / 4
The result of executing r twice
The code is as follows:
Var reg=/\ dswag while (r=reg.exec ('a1b2c3')) {console.log (r.index+':'+r [0]);}
You can see the results:
The code is as follows:
1:13:25:3
In addition to the above two methods, some string functions can be passed in the RegExp object as parameters to perform some complex operations
StrObj.search (RegObj)
The search () method is used to retrieve a substring specified in a string or to retrieve a substring that matches a regular expression. The search () method does not perform global matching, it ignores the flag g. It also ignores the lastIndex attribute of regexp and always retrieves from the beginning of the string, which means that it always returns the first matching position of the stringObject.
The code is as follows:
'a1b2c3'.search (/\ dgamg); / / 1'a1b2c3'.search (/\ d /); / / 1
StrObj.match (RegObj)
The match () method retrieves the string stringObject to find one or more text that matches the regexp. However, whether regexp has the mark g or not has a great influence on the results.
If regexp does not have a flag g, then the match () method can only perform a match once in the strObj. If no matching text is found, match () returns null. Otherwise, it returns an array of information about the matching text it finds. The 0th element of the array holds matching text, while the rest of the elements hold text that matches the child expression of the regular expression. In addition to these regular array elements, the returned array contains two object properties. The index attribute declares the position of the starting character of the matching text in the stringObject, and the input attribute declares a reference to the stringObject.
The code is as follows:
Var r='aaa123456'.match (/\ d /)
If regexp has the flag g, the match () method performs a global search to find all matching substrings in the strObj. If no matching substring is found, null is returned. If one or more matching substrings are found, an array is returned. However, the contents of the array returned by the global match are quite different from the former, in which all the matching substrings in strObj are stored in the array elements, and there are no index or input attributes.
The code is as follows:
Var r='aaa123456'.match (/\ dUnip g)
StrObj.replace (regObj,replaceStr)
With regard to the replace method of the strng object, we most commonly use the method of passing in two strings, but this method has a flaw that it can only replace once.
The code is as follows:
'abcabcabc'.replace (' bc','X'); / / aXabcabc
The first parameter of the replace method can also be passed into the RegExp object, and the replace method is more powerful and flexible when you pass in regular expressions.
The code is as follows:
'abcabcabc'.replace (/ bc/g,'X'); / / aXaXaX'abcaBcabC'.replace (/ bc/gi,'X'); / / aXaXaX
If the first parameter of the replace method passes in a grouped regular expression, we can get the grouped content in the second parameter using $1. Room9. For example, we want to replace the string 13489 with $# xexamples. We can do this
The code is as follows:
'13489'.replace (/ / gjingjinghuang) / / 1mpg / 567 / 89 / 341 / 567 / 89
Of course, there are many ways to achieve this, just to demonstrate the use of grouping content, we use @ # $1 packets in the second parameter, where $1 represents the captured grouping content, which is often seen in some js template functions to replace strings.
StrObj.replace (regObj,function () {})
You can make replace more powerful by modifying the second parameter of the replace method. In the previous introduction, you can only replace all matches with fixed content, but what if I want to wrap all the numbers in a string in parentheses
The code is as follows:
'2398rufdjg9w45hgiuerhg83ghvif'.replace (/\ dbadge gjurisdiction function (r) {return' ('+ ringing')';}); / / "(2398) rufdjg (9) w (45) hgiuerhg (83) ghvif"
Pass the second parameter of the replace method to a function. The function is called every time it matches and replaces, which is a callback function for each replacement. We use the first parameter of the callback function, that is, the matching content. In fact, the callback function has a total of four parameters.
1. The first parameter is very simple. It matches the string.
two。 The second parameter is the regular expression grouping content. If there is no grouping, there is no such parameter.
3. The third parameter is the index of the match in the string
4. The fourth parameter is the original string.
The code is as follows:
'2398rufdjg9w45hgiuerhg83ghvif'.replace (/\ dbadge grecedence function (arecedence bpenc) {console.log (axiom'\ tresume 2398rufdjg9w45hgiuerhg83ghvif45 12 2398rufdjg9w45hgiuerhg83ghvif83 22 2398rufdjg9w45hgiuerhg83ghvif); return'('+ aplomb')';}); 10 2398rufdjg9w45hgiuerhg83ghvif45 12 2398rufdjg9w45hgiuerhg83ghvif83 22 2398rufdjg9w45hgiuerhg83ghvif
This is the case where there is no grouping. What is printed is the matching content, the matching item index and the original string. Look at an example of grouping. If we want to remove the shell of a string, it will be 123.
The code is as follows:
'' .replace (/] +)% > / gMagneFunction (arecedence brecedence crecedence d) {console.log (arecedence brecedence crecinct) {console.log (arecalypse'\ tprecinct citation'\ treceduld); return b;}) / / 12310 2 5 3 10
Many powerful functions can be achieved according to this parameter replace, especially in complex string substitution statements.
StrObj.split (regObj)
We often use the split method to split strings into character arrays
The code is as follows:
'arecalculum brecediccrecord.split (','); / / ["a", "b", "c", "d"]
Similar to the replace method, we can use regular expressions to solve the problem in some complex segmentation cases
The code is as follows:
'a1b2c3d'.split (/\ d /); / / ["a", "b", "c", "d"]
In this way, you can split the string according to the number, which is not very powerful. After reading these two blogs, you will basically be able to deal with the usual JavaScript regular expressions. You are required to capitalize the first letters of an English paragraph in a div. Do you know what to do?
These are all the contents of the article "how to use JavaScript in regular expressions". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.