JavaScript learning notes _ String method used for pattern matching, learning notes string

Source: Internet
Author: User

JavaScript learning notes _ String method used for pattern matching, learning notes string

String Method Used for pattern matching:

String supports four regular expressions:

Seach () is used for retrieval. The parameter is a regular expression that returns the position of the first matched substring. If the parameter is not a regular expression,-1 is returned, the RexExp constructor is used to convert the RexExp to a regular expression. The seach () method does not support global search. It ignores the modifier g;

Replace () is used for retrieval and replacement operations. The first parameter is a regular expression, and the second parameter is the string to be replaced. It searches the strings that call this method and replaces them with the second parameter according to the pattern matching substring. If the modifier g is contained, the full-text match is performed. If the first parameter is not a regular expression but a string, search and replace the string;

Text. replace (/javascript/gi, 'javascript ') indicates to replace the case-insensitive JavaScript search in text with javascript

If the string to be replaced appears ($ plus a number), The replace () method will replace the text that matches with the child expression of $ plus a number, for example:

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

Text. replace (quote, '"$1"'); full-text search and/"([^"] *) "/matched string, replace with "$1" (meaning that only the double quotation marks on both sides are changed to the Chinese half width, while the content stored in $1 remains unchanged) $1 stores a string that matches the child expression in the first left parentheses;

The unique match () parameter is a regular expression that returns an array composed of matching results. If the parameter is not a regular expression, first, the RexExp constructor will convert it into a regular expression;

(2) If this regular expression carries the modifier g, the array elements returned by match () are all strings matching the full text;

(2) If this regular expression does not contain the modifier g, match () Only retrieves the first match, but it also returns an array (the first element of the array is the matching string, the following element is the string matched by the Child expression enclosed by parentheses in the expression. Each element corresponds to a bracket)

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 arrays composed of multiple substrings according to the delimiter specified by the parameter, for example:

'123'. split (','); // return ['123', '123', '123']

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

The above is all the content of the String method used for pattern matching. I hope it will be helpful to you ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.