Operation of the JS string

Source: Internet
Author: User

JS string operation is still very frequent, if the same regular expression combined, the function is very powerful.

1.test

Verifies that the string conforms to a regular expression.

/\s+/.test ("I ' M You");  // true

It will only return true and false;

2.Match

Retrieves the specified value within the string, or finds a match for one or more regular expressions.

Matches to a value that returns an array, otherwise returns NULL.

The use of the 2.1 match is very much related to the G flag of the regular expression, and if there is no g in the regular , then the No. 0 element holds the matching text.

Also contains two object properties. The Index property is a reference to the input property string that matches the position in the starting character of the text.

var str= "1 plus 2 equal 3"Console.log (Str.match (/\d+///["1", index:0, Input: " 1 plus 2 equal 3 "]

Note: If you need to get to the Index property, it should be:

Str.match (/\d+/). Index.

If there are groups in the regular expression:

var str= "1 plus 2 equal 3"Console.log (Str.match (/(\d) \s+ (eq) +/)); // ["2 eq", "2", "EQ", Index:7, Input: "1 plus 2 equal 3"]

In addition to the parts of the array that match the regular expression, there are also parts of the sub-expressions.

2.2 If there is a G flag in the regular expression, the match () method performs a global retrieval and finds all matching substrings.

var str= "1 plus 2 equal 3"Console.log (Str.match (/\d+/g)); // ["1", "2", "3"]

Match cannot get the value of multiple sub-expressions, want to get can be used with exec.

3.exec

When calling the Exec () method of a non-global REGEXP object, the returned array is the same as the array that is returned by the calling method match ().

The difference is that when the regular expression is global, there is a lastindex property that matches the next position of the last character of the text.

var str= "1 plus 2 equal 3 2 Ep3"; var regex=/(\d) \s+ (E[QP]) +/G;console.log (Str.match (regex));   // ["2 eq", "2 EP"] Console.log (regex.lastindex); // 0 Console.log (regex.exec (str)); // ["2 eq", "2", "EQ", Index:7, Input: "1 plus 2 equal 3 2 Ep3"] Console.log (regex.lastindex); //  One

In order to get to all the groupings, you can do this:

var str= "1 plus 2 equal 3 3 ep3"; var regex=/(\d) \s+ (E[QP]) +/G,result;  while NULL ) {   console.log (result);   Console.log (Regex.lastindex);} // ["2 eq", "2", "EQ", Index:7, Input: "1 plus 2 equal 3 3 ep3"] //  One // ["3 EP", "3", "EP", Index:17, Input: "1 plus 2 equal 3 3 ep3"] //  +

4.replace

Used to replace characters with some characters in a string.

the $ character in replacement has a specific meaning. As shown in the following table, it shows that the resulting string from a pattern match will be used to replace

$, $ 、...、 The text that matches the 1th to 99th sub-expression in RegExp.
$& A substring that matches the regexp.
$` The text that is located to the left of the matching substring.
$ The text on the right side of the matching substring.
$$ Direct volume symbol.

For example: Replace the u=32 in the URL with the u=32-8;

var u = 8; Window.location.href.replace (/[?&]u= ([^&]+)/,function($0,$1) {      return $0.replace ($, $1+ '-' +U);})

Operation of the JS string

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.