Home  >  Article  >  Web Front-end  >  The functions of substr() and match() in js

The functions of substr() and match() in js

一个新手
一个新手Original
2017-09-07 09:56:502965browse

Study the project code and come across a few String and Array methods that have not been used much recently. Review them:
1. substr()
Usage: used in strings Extract the specified number of characters starting from the start subscript;
Return value: a new string containing length characters starting from the start of string (including start);
Syntax: String.substr(start ,length);
Parameter start: required; the starting index of the string to be extracted. Must be a numeric value. If it is a negative number, then this parameter represents the position starting from the end of the string. For example, -1 refers to the last character in the string, -2 refers to the second to last character in the string, and so on.
Parameter length: optional; the number of characters in the substring to be extracted must be a numerical value. If omitted, the substring starting from the String start position to the end will be returned.
Note: The parameters of substr() specify the starting position and length of the substring, so it can be used instead of substring() and slice().
Important: ECMAscript does not standardize this method and therefore discourages its use.

2. match():
Function: Retrieve a specified value in a string, or find a match for one or more regular expressions; similar to indexOf() and lastIndexOf(), but match( ) returns the specified value, not the position of the string;
Syntax:
match(searchvale);
match(regExp);


The above is the detailed content of The functions of substr() and match() in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn