In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "String string matching javascript regular expressions". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
There are four ways to support regular expressions in String objects: search, replace, match, and split
Str.search (regexp)
Definition: the search () method retrieves the string that matches the expression regexp in the string str and returns the position of the first character of the first matching string. If no matching string is found,-1 is returned.
For example:
Var str = "Javascript"; str.search (/ script/); / / returns the position of s in script as 4str.search (/ jthumb I); / / sets the regular expression to identify I: ignore case, match to J and return position 0
However, the search () method does not support global search because the identification g of the regular expression parameter is ignored, and the lastIndex property of regexp is also ignored, always retrieving from the beginning of the string, so it always returns the first matching position of the str.
For example:
Var str = "javascript is cool"; str.search (/ splanchg); / / returns s position 4 in javascript. S str.replace (regexp, replacement) will not be retrieved.
Definition: the replace () method performs the find and replace operation. It matches the string to which the regular expression regexp matches, and then replaces the string with the string of replacement. If there is a global identity g, it replaces all matching strings, otherwise only the first matching string is replaced.
The replace method should be a more commonly used method and is very useful in string replacement operations. For example:
1. Simple replacement
Var str = "javascript"; str.replace (/ javascript/,'JavaScript'); / / replace the string javascript with JavaScript
2. Global replacement
/ / use the global representation g to globally replace var str = "javascript"; str.replace (/ a Universe g,'b'); / / replace all letters a with the letter b and return jbvbscript
3. Use the special character replacement in replacement. The $character has a special meaning in replacement, as shown in the following table:
Let's take a look at the example:
/ / 1. Replace with subexpressions: var str = "javascript", such as $1, $2, etc.; str.replace (/ (java) (script) /,'$2 $1'); / / in the expression () is a subexpression, and $1 corresponds to the content of the first expression, that is, java,$2 is script, so the replacement result is: scriptjava / / 2, $& the string var str = "javascript" matched by positive expressions; str.replace (/ java/,'$&-') / / the regular expression is matched by the direct quantity java, and the matching result is java, then the value of $& is java, and then replace the matching string with the string $& -. The result is java-script// 3, $`$'$$var str = "javascript"; str.replace (/ ava/, "$`"); / / $`is the text on the left side of the matching substring ava, then the result of the replaced ava is: jjscriptstr.replace (/ ava/, "$'") / / $'is the right text of the matching substring ava, then script, the result of replacing ava is: jscriptscriptstr.replace (/ ava/, "$$"); / / $$is a direct measure symbol, that is, a $symbol is inserted, and the replacement result is: j$script
4. Use replacement as a function replacement
Replace's argument replacement can be a function rather than a string, which is called for each match, and the string it returns is used as the replacement text. The first argument to this function is the string that matches the entire pattern, and the next parameter is the string that matches the subexpression in the pattern, which can have 0 or more parameters. The next parameter is an integer that declares where the match occurs in the str. The final parameter is str itself.
Let's look at an example:
/ / match matches the entire string, that is, 'abc12345#$*%'// p1 is the first subexpression, ([^\ d] *), matches 0 or more non-numeric characters, that is, abc// p2 is the second subexpression, (\ d *), matches 0 or more numbers, that is, 12345 / p3 is the third subexpression, ([^\ w] *), matches 0 or any non-word characters. Equivalent to'[^ A-Za-z0-9 _]', that is, # $*% / / offset is the position where the pattern match occurs. If the match has been successful from the first character, then the position is 0 string is the string itself, that is, abc12345#$*%function replacer (match, p1, p2, p3, offset, string) {return [p1, p2, p3] .join ('-') } var newString = 'abc12345#$*%'.replace (/ ([^\ d] *) (\ d *) ([^\ w] *) /, replacer); / / the replacement result is: abc-12345-# $*%
Str.match (regexp)
Definition: the match () method is the most commonly used String regular expression method. Its only argument is a regular expression or a regular expression created through the RegExp () constructor, and the return value is an array containing the matching results.
The regexp regular expression in the match () method is generally divided into two cases: the global flag g is set and the global flag is not set.
1. Set the global flag
If the global flag g is set, the returned array contains all the matching results that appear in the string, for example:
/ / Global matching var str = "1 plus 2 equals 3"; str.match (/\ dpacg); / / matches all the numbers that appear in the string and returns an array: [1mem2pence3]
2. No global flag is set
If the global flag is not set, it is not a global search, just the first match. In this case, the matching result of the match () method also returns an array, the first element of the array is the matching string, and the remaining elements are subexpressions enclosed in parentheses in the regular expression. Let's look at an example:
/ / non-global matching var str = "visit my blog at http://www.example.com";str.match(/(\w+):\/\/([\w.]+)/); / / return result: ["http://www.example.com"," http "," www.example.com "] / / the result of regular expression matching is: first subexpression of http://www.example.com// (\ w +) matching result: second subexpression of http// ([\ w.]) Matching result: www.example.com
Str.split (delimiter, limit)
Definition: the split () method can decompose the calling string into an array of strings, using a delimiter as its argument.
Parameters:
Delimiter: a string or regular expression that splits the string from the place specified by this parameter.
Limit: specifies the maximum length of the returned array, and if this parameter is not set, the entire string will be split.
For example:
/ / 1. Only one parameter is passed. By default, the whole string is divided into var str = "a, b", "c", "d", "e"] / 2. By default, the whole string is divided into var str = "a, b", "c", "d", "e"] / 2. Two parameters are passed in: str.split (',', 3) / / if the specified length is specified, the corresponding array is returned: ["a", "b", "c"] / 3, using regular expression matching without the segmentation string var str = "aa44bb55cc66dd"; str.split (/\ dlength /); / / splitting the string by matching numbers but not containing the split string, the result is ["aa", "bb", "cc", "dd"] / / 4. Use regular expression matching, including the segmentation string var str = "aa44bb55cc66dd"; str.split (/ (\ d +) /) / / split the string by matching numbers, and the segmented string is contained in the subexpression, then the returned result is: ["aa", "44", "bb", "55", "cc", "66", "dd"] "String string matches the regular expression of javascript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.