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 with you a detailed tutorial ...
Yesterday when I was implementing the function of...
Table of contents Preface Child components pass d...
1. Change the Host field value of a record in the...
Several Differences Between MySQL 5.x and MySQL 8...
This article example shares the specific code of ...
I installed a new version of MySQL (8.0.21) today...
During today's lecture, I talked about the di...
Table of contents definition structure Examples C...
Query the current date SELECT CURRENT_DATE(); SEL...
background Some time ago, our project team was he...
This article shares the specific code of js to ac...
The first time I used the essay, I felt quite awkw...
Preface I once encountered a difficult problem. I...
summary In some scenarios, there may be such a re...