English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Learning Notes: String Methods for Pattern Matching

String methods used for pattern matching:

String supports4Types of regular expression usage:

seach() is used for search, the parameter is a regular expression, and it returns the position of the first substring that matches it. If not found, it returns-1If the parameter is not a regular expression, it will first convert it to a regular expression through the RegExp constructor, and the seach() method does not support global search, it ignores the modifier g;

replace() is used for search and replace operations, the first parameter is a regular expression, and the second parameter is the string to be replaced. It searches for and replaces substrings in the string called by the method according to the pattern, replacing them with the second parameter. If the modifier g is included, it performs a full-text match. If the first parameter is not a regular expression but a string, it will directly search and replace the string;

text.replace(/javascript/gi,'JavaScript') means searching for and replacing 'javascript' with 'JavaScript' in the entire text of text without case sensitivity

If the string to be replaced contains ($ followed by a number), the replace() method will use the text matched by the subexpression indicated by $ plus the number to replace it, for example:

var quote = /"([^"])*"/g

text.replace(quote,'"')1Full text search with/"([^"])*"/matches the string, and replace it with “$1”(indicating that only the double quotes on both sides are changed to Chinese half-angle, and stored in1(unchanged inside the content)1stores the content that refers to the string matched by the first left parenthesis subexpression;   

match() takes a single parameter, which is a regular expression, and returns an array composed of the matching results. If the parameter is not a regular expression, it will first be converted into a regular expression through the RegExp constructor;

(II) If this regular expression has the modifier g, the array elements returned by match() are all the strings matched in the full text;

(II) If this regular expression does not have the modifier g, match() only searches for the first match, but it also returns an array (the first element of the array is the entire matched string, and the subsequent elements are the strings matched by all subexpressions enclosed in parentheses in the expression, each element corresponds to a parenthesis)

For example:

var url = /(\w*):\/\/([\w.]+)\/(\S*)/;
  var text = 'http://www.w3school.com.cn/jsref';
  var result = text.match(url);
  if(result != null) {
  var fullurl = result[0];
  var protocol =result[1];
  var host = result[2];
  var path = result[3];

split() splits a string into an array of substrings according to the separator specified by the parameter, such as:

123,345,789'.split(','); //return['123345789

1,   2  , 3, 4   ,5  ,  6'.split(',/\s*,\s*/);  //return['123456

This is the full content of the JavaScript learning notes compiled by the editor for pattern matching of the String method, I hope it will be helpful to everyone, please support the呐喊 tutorial~

You May Also Like