In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 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 String method in regular expression pattern matching, which has certain reference value. Interested friends can refer to it. I hope you will have a lot of gains after reading this article. Let Xiaobian take you to understand it together.
Using regular expressions for pattern matching in JavaScript code often uses some methods of String objects and RegExp objects, such as replace, match, search, etc. The following is a summary of some methods used.
String objects support four methods for regular expressions: search, replace, match, split
str.search(regexp)
Definition: The search() method retrieves the string str that matches the expression regexp and returns the position of the first character of the first matching string. If no match is found, returns-1.
For example:
var str = "Javascript";str.search(/script/); //Returns the position of s in script as 4str.search (/j/i); //Sets the regular expression to identify i: ignore case, match to J, return position 0
However, the search() method does not support global search because it ignores the identity g of the regular expression parameter and also ignores the lastIndex attribute of regexp, always retrieving from the beginning of the string, so it always returns the first matching position of str.
For example:
var str = "javascript is cool";str.search(/s/g); //Returns s position 4 in javascript, does not continue to retrieve s
str.replace(regexp, replacement)
Definition: The replace() method performs a 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, replacing all matching strings if there is a global identifier g, otherwise replacing only the first matching string.
The replace method should be one of the more commonly used methods, which is very useful in string replacement operations. For example:
1. Simple replacement
var str = "javascript";str.replace(/javascript/,'JavaScript'); //Replace string javascript with JavaScript
2. Global replacement
//global substitution with global representation g var str = "javascript";str.replace(/a/g, 'b'); //replaces all letters a with letters b, returning jbvbscript
3. Use the special character replacement in replacement. The $character in replacement has a special meaning. The details are as follows:
Character replacement text $1,$2,...,$ 99 text matching the 1st to 99th subexpressions in regexp $& string matching regexp $`text to the left of matching substring $'text to the right of matching substring $$literal symbol
Let's look at an example:
//1, Subexpression replacement: $1,$2, etc. var str = "javascript";str.replace(/(java)(script)/,'$2$1'); //In the expression () is a subexpression,$1 corresponds to the content of the first expression, i.e. java,$2 is script, so the replacement result is: scriptjava //2,$& is the string matching the positive expression var str = "javascript";str.replace (/java/,'$&-'); //regular expressions match java directly, if java matches, then $& has java, then replace the matching string with the string $&-, java-script// 3,$` $' $$var str = "javascript";str.replace (/ava/,"$`"); // $` is the left text of the matching substring ava, then j, then the result of ava after replacement is: jjscriptstr.replace (/ava/,"$'"); // $' is the text on the right side of the matching substring ava, then script, then the result of replacing ava is: jscriptscriptstr.replace(/ava/,"$$"); // $$is the direct quantity symbol, that is, insert a $symbol, the result of replacement is: j$script
4. Use replacement as a function replacement
The parameter replacement of replace can be a function rather than a string, and every match calls the function, which returns a string that is used as the text of the replacement. The first argument to this function is a string that matches the entire pattern, the next arguments are strings that match subexpressions in the pattern, and there can be zero or more arguments. The next argument is an integer that declares where the match occurs in str. The final argument is str itself.
Here's an example:
// match matches the entire string, i.e.:'abc12345#$*%'// p1 is the first subexpression,([^\d]*) matches 0 or more non-numeric characters, i.e.:abc// p2 is the second subexpression,(\d*) matches 0 or more numeric characters, i.e.: 12345// p3 is the third subexpression,([^\w]*) matches 0 or matches any non-word character. Equivalent to '[^A-Za-z0 -9_]', i.e.#$*%// offset is the position where pattern matching occurs, since the first character has been successfully matched, the position is 0// string is the string itself, i.e. abc12345#$*%function displacer (match, p1, p2, p3, offset, string) {return [p1, p2, p3].join(' - ');}var newString = 'abc12345#$*%'.replace (/([^\d]*)(\d*)([^\w]*)/, displacer); //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 by the RegExp() constructor, and the return value is an array containing the matching results.
The regexp regular expression in match() method is generally divided into two cases: with and without global flag g
1, there is a global flag set
If the global flag g is set, the returned array contains all matches that occur in the string, for example:
//matches globally var str = "1 plus 2 equals 3";str.match(/\d/g); //matches all numbers that occur in a string and returns an array: [1,2,3]
2. No global flag is set
If the global flag is not set, it is not a global search, only the first match is retrieved. In this case, the match() method returns an array whose first element is the matching string and whose remaining elements are bracketed subexpressions of the regular expression. Here's an example:
var str = "visit my blog at http://www.example.com";str.match(/(\w+):\/\/([\w.]+)/); //Return result: ["http://www.example.com", "http", "www.example.com"]//Regular expression matching result: www.example.com// First subexpression (\w+) Matching result: http//Second subexpression ([\w.]) Match Results: www.example.com
str.split(delimiter, limit)
Definition: The split() method can split the string that calls it into an array of strings, using delimiters as its parameters.
Parameters:
delimiter: String or regular expression that splits the string from the place specified by this parameter.
limit: Specifies the maximum length of the returned array. If this parameter is not set, the entire string will be split.
For example:
//1. Pass only one parameter, split the whole string by default var str ="a,b,c,d,e";str.split (','); //Return the split string array: ["a", "b", "c", "d", "e"]//2. Pass two parameters var str ="a,b,c,d,e";str.split (',',3); //Specify a limited length, and return the corresponding array: ["a", "b", "c"]//3. Use regular expression matching, not including split string var str = "aa44bb55cc66dd";str.split(/\d+/); //Split string by matching digits, but not including split string, the returned result is: ["aa","bb","cc","dd"];//4. Use regular expression matching, including split string var str = "aa44bb55cc66dd"; str.split(/(\d+)/); //Splits a string by matching digits, and the split string is contained in a subexpression, the result returned is: ["aa", "44", "bb", "55", "cc", "66", "dd"] Thank you for reading this article carefully. I hope that Xiaobian will share the article "How to use String method in regular expression pattern matching". This article is helpful to everyone. At the same time, I hope everyone will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!
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.