JavaScript Basics---Regular expressions and strings, arrays

Source: Internet
Author: User

First, regular expression

1. How to create a regular expression

var re=/abc/;

var re=new RegExp ("abc")

var re=new RegExp (/abc/);

2. Regular expression syntax

2.1 Simple syntax

c denotes one or more ordinary characters

{n}: Indicates the number of matches

{N,}: Indicates the number of matches =n

{N,m}: Indicates the number of matches N,<m

\: Indicates the number of escapes

^: The starting position of the string that matches the input (matches at the beginning of the matched string)

$: Indicates the end position of the matching input string (matched at the end of the matched string)

*: "= 0;

+:>=1;

?: 0 or 1; when he is combined with other characters, it is a non-greedy match.

These are all matched to one character:

X|y: Indicates that a string match x or y is counted to match success

[XYZ]: Represents the ability to match any character in XYZ eg: matches the x in extend.

[^XYZ] can match characters other than XYZ.

[X-z] can match any one of the x-z characters EG:[A-C]==[ABC]

Non-greedy pattern matching

Is after the number of times +?

Regular expression Escape syntax

\b: Matches a word, but the word must be on the boundary eg: the ABC match on the/ABC\B/DJFABCOWEABC boundary

\b: Matches a word and does not have a word on the boundary

\d: Matches a numeric character in 0-9

\d: Matches a non-numeric character, which is a character other than 0-9.

\s: Indicates that a white space character is matched.

\s: Indicates that a non-whitespace character is matched.

\w: Indicates that any one word character is matched, indicating a numeric letter underline.

\w: Indicates that any non-word characters are matched.

3. Use EXEC () and test () to match strings after creating a regular expression

EXEC (): is an array that matches a string and returns the result that contains the match. Text () is a string that matches after a match is returned.

The returned array has: input: The string to match, index: The position of the substring where the fart amount begins, LastIndex: The position of the last substring being matched

Source: Mode

Test (): Returns True or false.

Two. String

Correlation functions for strings:

CharAt (i) eg:str.charAt (i) indicates the I-character of the returned STR string.

charCodeAt (i) str.charcodeat (i) is a Unicode code value that returns the I-character of the STR string.

Concat (STR1,STR2), Str3=concat (ST1,STR2);

IndexOf (str,sub) returns the position of the sub in Str for the first time, or 1 if not found

LastIndexOf (str,sub), find the position of the sub last match in Str.

Slice () extracts part of a string.

Splice () Str.splice (","), the string is separated, and the array is returned.

Match (RE) Str.match (Regular object | mode), finds the regular object, does not return null otherwise returns an array

Replace (Re,retext) str.replace (Reg, "abcd") is searched through a regular lookup and, if found, is replaced with ABCD, returning the replaced copy.

Search (re) searches for a re, and then returns the distance to the beginning of the string, otherwise 1

Three, array

var arr=new Array ();

Arr.isarray () Determine if arr is an array

Common functions:

(1) Insert, delete

Pop () var e=arr.pop () means trailing delete, returning the current value

Push () var Len=arr.push () indicates a trailing insert, returning the current length

Shift () var E=ARR.SHITF () indicates that the header deletes an element, returning the value of the current element

Unshift () var len=arr.unshitf () indicates that an element is inserted in the head, returning the current length.

(2) iterate through each element in the array (ECMAScript 5) (fun1 can protect three elements, item index array, current element, subscript, array name)

Every (), perform a function test for each element in the array until the returned result value is false at the end. is no longer executed. Eg Arr.every (fun1). The elements in the ARR array are executed from the beginning with the person fun1, until the fun1 result is false.

Filter (), similar to every, executes all the items, puts the value of the item with the return value in the array, and returns the array

ForEach () invokes a callback function for each element in the array. Arr.foreach (Fun1,this) uses the FUN1 function for each element in arr. This points to yourself and can be omitted.

Map () is similar to every (), where each element in the array calls the test function, puts the result returned by each item in the array, and returns the array. Arr.map (FUN1);

Some () perform a function test for each element in the array until the return value of the result is true.

Reduce () from left to right

Reduceright () from right to left

(3) Array links

Concat () returns a new array

Join () returns a string

(4) Find array subscript

IndexOf () uses the subscript arr.indexof ("a"), which is equal to the match, to return the subscript, otherwise returns-1

LastIndexOf () subscript from the back forward match

(5) Sort

Sort (). By default, it is sorted in ascending order and uppercase is better than lowercase. Within the custom sort, 1 indicates that the first parameter is smaller than the second argument. 0 means two parameters are equal. 1 indicates that the first parameter is larger than the second argument.

SortOn () means sorting by 1 or more fields.

Reverse () reverses an array

(6) Array cloning

Shallow cloning: When an element in an array is still a complex data type, it is not copied, but a reference to the original data is created;

Deep cloning: reads data from the reference type in the original array and populates the array with the new one.

JavaScript Basics---Regular expressions and strings, arrays

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.