What are the JavaScript string interception methods?

coldplay.xixi
Release: 2020-06-30 14:34:20
Original
7034 people have browsed it

JavaScript string interception methods include: 1. slice method, the string is intercepted from left to right; 2. substring method, when the first parameter is a negative integer, the entire string is intercepted; 3. The match method returns an array containing matching strings.

What are the JavaScript string interception methods?

JavaScript string interception methods are:

1. Slice method:

Note

1. Strings are intercepted from left to right, and there will be no interception from right to left;

2. The slice and substring methods intercept the returned characters. The string contains numStart but not numEnd;

3. Note that the slice method intercepts the empty string when numStart is greater than numEnd and the substring callbacks the two positions when numStart is greater than numEnd and intercepts the string between numEnd and numStart

var str = "HellohappyworldHello!"; //1.slice(numStart,numEnd)
Copy after login

The returned value is to intercept the string between the specified subscripts. The parameter can be a negative number or left blank. A positive integer means reading from left to right, and a negative integer means reading from right to left. Subscript interception, when the first starting subscript parameter is a negative integer,

The second parameter is not needed. If you write an empty string regardless of whether it is a positive integer or a negative integer, it is summarized as follows , when the parameters are not filled in, the original string is intercepted. When the first parameter is a positive integer, the second parameter

does not need to be filled in (that is, to the end of the string), or a positive integer ( That is, from start-end), it can be a negative integer (that is, start-(str.length end)),

When the first parameter is a negative integer (that is, read the subscript from right to left, intercept it from the right starts from -1), the second parameter will be intercepted to be empty. Finally, please note that the second parameter cannot be larger than the first parameter, which can be compared with the coordinate axis

6 var sliceStr1 = str.slice(2); //llohappyworldHello! 7 var sliceStr2 = str.slice(2,7); //lloha 8 var sliceStr3 = str.slice(-2); //o! 9 var sliceStr4 = str.slice(-2,5); //"" 10 var sliceStr5 = str.slice(-2,-5); //"" 11 var sliceStr6 = str.slice(2,-5); //llohappyworldH 12 console.log(sliceStr1); 13 console.log(sliceStr2); 14 console.log(sliceStr3); 15 console.log(sliceStr4); 16 console.log(sliceStr5); 17 console.log(sliceStr6); 18 19 //2.substr(numStart,length)
Copy after login

2. substr Method:

returns a string of length intercepted from the specified position. numStart is required. When it is a positive integer, it means reading the subscript from left to right. When it is a negative integer, , which means reading the subscript from right to left and intercepting.

The second parameter indicates the length of the string that needs to be intercepted. When it is a negative integer, the returned value is empty. When it is an integer, it represents the intercepted length. When When the remaining length of the string is exceeded, it goes to the end of the string

20 var substrStr1 = str.substr(2); //llohappyworldHello! 21 var substrStr2 = str.substr(-2); //o! 22 var substrStr3 = str.substr(2,18); //llohappyworldHello 23 var substrStr4 = str.substr(-2,1); //o 24 var substrStr5 = str.substr(-2,-1); //"" 25 console.log(substrStr1); 26 console.log(substrStr2); 27 console.log(substrStr3); 28 console.log(substrStr4); 29 console.log(substrStr5); 30 31 //3、substring(numStart,numEnd)
Copy after login

3. Substring method:

Similar to slice, but when the first parameter is a negative integer , the entire string is intercepted

32 var substringStr1 = str.substring(2); //llohappyworldHello! 33 var substringStr2 = str.substring(-2);//HellohappyworldHello! 34 var substringStr3 = str.substring(7,2); //lloha 35 var substringStr4 = str.substring(2,7); //lloha 36 console.log(substringStr1); 37 console.log(substringStr2); 38 console.log(substringStr3); 39 console.log(substringStr4);
Copy after login

4. Match method:

Note

1. The match method returns a character containing the matching item Array of strings;

2. Regular expressions need to be written according to different situations;

3. The first one in the returned array is always the original string;

4 . When the regular expression has sub-expressions (that is, the content within (\S*) brackets), and when it is a global match, it only searches for the full matching regular expression and returns all the content. When it is a non-global match, it returns multiple elements. Array;

When the regular expression has no subexpressions and it is a global match, an array of multiple elements is returned. If it is a non-global match, the array that matches the first element is returned. Array

42 var regStr1 = str.match(/Hello/g); //["Hello","Hello"] 43 var regStr9 = str.match(/Hello/); //["Hello"] 44 var regStr2 = str.match(/hello/g); //null 45 var regStr3 = str.match(/Hello(\S*)world/); //["Hellohappyworld", "happy"] //截取中间字符串 46 var regStr4 = str.match(/(\S*)world/g); //["Hellohappyworld"] //截取指定字符之前的字符串 47 var regStr5 = str.match(/(\S*)world/); //["Hellohappyworld", "Hellohappy"] //截取指定字符之前的字符串 48 var regStr6 = str.match(/Hello(\S*)/g); //["HellohappyworldHello!"] //截取指定字符之后的字符串 49 var regStr7 = str.match(/Hello(\S*)/); //["HellohappyworldHello!", happyworld!] //截取指定字符之后的字符串 50 var regStr8 = str.match(/llo(\S*)/); //["llohappyworldHello!", happyworldHello!] //截取指定字符的字符串 51 console.log(regStr1); 52 console.log(regStr9); 53 console.log(regStr2); 54 console.log(regStr3); 55 console.log(regStr4); 56 console.log(regStr5); 57 console.log(regStr6); 58 console.log(regStr7); 59 console.log(regStr8); 60 
Copy after login

Of course, the above methods and usage are simple requirements. When the requirements in the project are more complex, they must be intercepted based on the actual situation, but no matter how they are used, the methods or implementation ideas are generally the same. Similarly,

In addition, I also encourage myself that a good memory is not as good as a bad writing. If you encounter any problems or new knowledge, you should develop the habit of recording it. I hope to encourage you all.

Related learning recommendations:javascript video tutorial

The above is the detailed content of What are the JavaScript string interception methods?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!