JS string and regular match

Source: Internet
Author: User

Here is the specific use of the method, do not do too much explanation.

First look at the usual methods of strings:

1, concat (); Combine multiple text, return a new string, or stitch a string.

2, IndexOf (); Returns the index to match the first occurrence of the character in the string, the argument is a matching character, and no return-1.

3, Chatat (); Returns the position of the specified character.

4, LastIndexOf (); Returns the index to match the last occurrence of the character in the string, the argument is a matching character, and no return-1.

5, Match (); check whether a string matches a regular.

6. Replace (); finds a matching string and replaces the matching string with a new string.

7. Search (); Regular match lookup, if the lookup succeeds, returns the matching index in the string, otherwise returns-1.

8, substr (); intercepts a string, the first argument begins, the second is truncated, the second parameter is negative, and returns NULL.

9, substring (); intercepts a string, the first argument is the start position, the second end position (not included), the negative argument is converted directly to 0, it is worth noting that the parameter has no successive points, small in the former large in the post.

10, Slice (); Intercept string, parameter: Start position, end position (not included).

11, split (); Adds a string to an array.

12, toLowerCase (); Turns the entire string into lowercase characters.

13, toUpperCase (); Converts a string into uppercase characters.

14. Length: Long attribute.

    

Methods for matching regular strings: str. Method (REG)

1, Str.search ()

The parameter is a regular, which will look up the character in the string that matches the regular match, and returns the position where the character first appears, without the return-1, which should be searched from the beginning for each search, so only the first occurrence is returned.

var str= "JavaScript"; Str.search (/script/); // returns 4

2, Str.replace ();

is a common substitution operation in JS.

A, simple replacement

var str = "JavaScript"; Str.replace ("/a/", "B"); // Replace the first a in the string with B

B, Global substitution

var str = "JavaScript"; Str.replace ("/a/g", "B"); // Replace all A in the string with B

3, Str.match ();

The most commonly used regular matching method, with only one regular parameter, returns a matching array of strings.

4, Str.split ();

Divide the array according to a character, there are two parameters (optional), the first is a matching item, either a character or a regular, and the second is the number of generated arrays. The second argument is empty, and the entire string is split.

var str = "1,2,3,4"; Str.split (","); ["1", "2", "3"]; var str1 = "aa11bb22cc"; Str1.split ("/\d+/"); [ AA,BB,CC] String segmentation based on numbers.  var str2 = "aa11bb22cc"; Str2.split ("/(\d+)/"); [AA,11,BB,22,CC]; is also divided by the array, but contains numbers.

Regular expression method to do matching Reg. method (str)

1, reg.test (str);

Checks if a string matches a regular match and returns a Boolean value

2, Reg.exec (str);

Returns the first value in a string that matches a regular match

3, Reg.compile (regexp);

Recompile the regular

JS string and regular match

Related Article

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.