Home > Web Front-end > JS Tutorial > JS string learning: split the string into substrings and pass them to the array

JS string learning: split the string into substrings and pass them to the array

青灯夜游
Release: 2021-08-25 16:00:06
Original
4591 people have browsed it

In the previous article "JS string learning: How to unify the upper and lower case of all characters", we introduced two methods to unify the upper and lower case of strings. Friends in need can learn and understand Take a look~

The main content of this article is "split the string into multiple smaller substrings, and pass these substrings to the array as array elements"; in simple terms, it is "string Convert to array".

So how to operate like this? We can use JavaScript’s built-in function split().

string.split(separator,limit) is used to split a string into a string array; accepts two omitted parameters separator (separator or Regular expression, specifying the split position) and limit (specifying the maximum length of the returned array)

The omitted parameter separator has multiple values, different The returned array is also different:

1. The split() method does not accept parameters:

var str="How are you doing today?";
var arr=str.split();
console.log(arr);
Copy after login

When the parameter is empty, the method will The string is returned as a one-element array (array length is 1 in this case). Therefore, the output result is:

JS string learning: split the string into substrings and pass them to the array

2. The separator parameter of the split() method is an empty string

var str="How are you doing today?";
var arr=str.split('');
console.log(arr);
Copy after login

When the separator parameter is When the string is empty, this method will split it according to single characters, and each character will be split, and a character array containing all single characters will be returned (in this case, the length of the array is the length of the string). Therefore, the output result is:

JS string learning: split the string into substrings and pass them to the array

3. The separator parameter of the split() method is a space ' '

var str="How are you doing today?";
var arr=str.split(' ');
console.log(arr);
Copy after login

When the separator parameter is a space, then this method will split according to the space and separate each word in the string as an array element. Therefore, the output result is:

JS string learning: split the string into substrings and pass them to the array

4. The separator parameter of the split() method is the regular expression

var str= "a2b3c4d5e678f12g";
var arr=str.split(/\d+/);  //把以匹配的数字为分隔符来切分字符串
console.log(arr);
Copy after login

When the separator parameter is Regular expression, this method can split using matching text as delimiter. For example, in the above example, the numbers from 0 to 9 are matched, and the numbers are used as separators, so the output result is:

JS string learning: split the string into substrings and pass them to the array

  • If the regular expression matches If the text is at the edge of the string, the split() method also performs the split operation and adds an empty array to the array.

var str= "122a2b3c4d5e678f12g456";
var arr=str.split(/\d+/);  //把以匹配的数字为分隔符来切分字符串
console.log(arr);
Copy after login

JS string learning: split the string into substrings and pass them to the array

  • #If the delimiter specified in the string is not found, an array containing the entire string is returned.

var str= "abcdefg";
var arr=str.split(/\d+/);  //把以匹配的数字为分隔符来切分字符串
console.log(arr);
Copy after login

JS string learning: split the string into substrings and pass them to the array

Specify the number of substrings included

If there are too many divided substrings, just want to To get the first few substrings, you can use the second parameter limit.

The second parameterlimit can specify the maximum length of the returned array, which means you can set the number of substrings included. If this parameter is set, the length of the returned array will not be greater than the value specified by this parameter:

var str="How are you doing today?";
var arr=str.split(' ',3);
console.log(arr);
Copy after login

JS string learning: split the string into substrings and pass them to the array

The parameter limit does not accept negative values. If a negative value is set, then This value will be ignored.

var str="How are you doing today?";
var arr=str.split(' ',-3);
console.log(arr);
Copy after login

JS string learning: split the string into substrings and pass them to the array

Okay, that’s all. If you need it, you can watch it: javascript video tutorial

The above is the detailed content of JS string learning: split the string into substrings and pass them to the array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template