JS provides three methods for intercepting strings:
Using a parameteralert(stmp.slice(3)); //Start from the 4th character and intercept to the last character; return "nn.cn" alert(stmp.substring(3)); //Start from the 4th character and intercept to the last character; return "nn.cn" Using two parametersalert(stmp.slice(1,5)) //Starting from the 2nd character, to the 5th character; returns "cinn" alert(stmp.substring(1,5)); //Starting from the 2nd character, to the 5th character; returns "cinn" If only one parameter is used and it is 0, then the entire parameter is returned.alert(stmp.slice(0)); //Return the entire string alert(stmp.substring(0)); //Return the entire string Returns the first characteralert(stmp.slice(0,1));//return "r" alert(stmp.substring(0,1));//return "r" //In the above example, we can see that the usage of slice() and substring() is the same, and the returned value is the same, but alert(stmp.slice(2,-5));//return "i" alert(stmp.substring(2,-5));//return "rc" //From the above two examples, we can see that slice(2,-5) is actually slice(2,3), negative 5 plus the string length 8 is converted to positive 3 (if the first digit is equal to or greater than the second digit alert(stmp.substring(1,5)) //Starting from the 2nd character to the 5th character; returns "cinn" alert(stmp.substr(1,5)); //Starting from the second character, intercept 5 characters; return "cinn."
phone.slice(-6) takes the last 6 digits (the second parameter does not need to be written as 0), and returns '012100 '; phone.slice(-6, -4) takes the last 4 digits to the last 6 digits, (-6+11, -4+11) = (5, 7); // Compare the date size. When the date is smaller than 1 every month, var nowdate = new Date(); item = 2016-7-16; temp = item.split('-'); if (temp[0] != curYear || temp[1] != curMonth) { return; } temp[1] = parseInt(temp[1]) + 1; date = new Date(temp.join('-')); if(date>=nowdate){ Execute A; }else{ Execute B; } Replace the letters after the specified string var abc = 'adadada=ss'; var j = abc.substring(abc.indexOf('=')+1,abc.length); var dsd = abc.replace(j,'haha'); --> dsd = 'adadada=haha' The above are three methods of JS string interception introduced by the editor. I hope it will be helpful to everyone. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
>>: Let's talk about the size and length limits of various objects in MySQL
This article shares the specific code of JavaScri...
Many people have encountered this error when star...
This article shares the specific code of js to ac...
Collapsed headers are a great solution for displa...
This article example shares the specific code of ...
Table of contents 1.watch monitors changes in gen...
Table of contents Parent component communicates w...
Use HTML to create complex tables. Complex tables...
1. Introduction When a web project is published o...
Data display has always been a demand that all wa...
Table of contents Purpose of Teleport How Telepor...
1. Install libfastcommon-1.0.43. The installation...
Click here to return to the 123WORDPRESS.COM HTML ...
1. What is Continuous Delivery The software produ...
During the development and debugging process, it ...