Usually the parameters of these APIs are mixed up, so intend to record down, when unsure of the time in the take out to turn over;
When doing the project, often will need to intercept the string, so commonly used methods have slice (), substr (), substring (), Match () method, etc., the use of four methods are as follows;
1<script type= "Text/javascript" >2 //methods for intercepting strings3 //Note 1, the interception of the string is from left to right, there will be no right-to-left interception, 2, slice and substring method, intercept the returned string contains Numstart and does not contain numend;3, Note that the slice method is truncated when Numstart is greater than numend and substring when Numstart is greater than numend, and intercepts the string between Numend and Numstart4 varstr = "Hellohappyworldhello!";5 //1.slice (numstart,numend) returns the value to intercept the specified subscript between the string, the parameter can be negative, can not be filled, positive integer means read from left to right, a negative integer to read the subscript from right to left intercept, when the first start subscript parameter is a negative integer, The second parameter does not need, if the write on whether a positive integer or a negative integer is truncated to an empty string, summarized as follows, when the parameter is not filled, the intercept is the original string, the first argument is a positive integer, the second argument, can not fill (that is, to the end of the string), can be filled with a positive integer (that Can be a negative integer (that is, start-(str.length+end)), the first parameter is a negative integer (that is, the right-to-left reading subscript intercept, the right is starting from 1), the second parameter is truncated to be empty, and finally note that the second parameter can not be greater than the first parameter, and an axis analogy6 varSLICESTR1 = Str.slice (2);//llohappyworldhello!7 varSLICESTR2 = Str.slice (2,7);//Lloha8 varSLICESTR3 = Str.slice (-2);//o! 9 varSLICESTR4 = Str.slice ( -2,5);//"" Ten varSLICESTR5 = Str.slice ( -2,-5);//"" One varSLICESTR6 = Str.slice (2,-5);//LLOHAPPYWORLDH A Console.log (SLICESTR1); - Console.log (SLICESTR2); - Console.log (SLICESTR3); the Console.log (SLICESTR4); - Console.log (SLICESTR5); - Console.log (SLICESTR6); - + //2.substr (Numstart,length) returns a string that is truncated from a specified position, a length of lengths, Numstart required, is a positive integer, is a left-to-right read subscript intercept, when a negative integer, the right-to-left reading subscript intercept, The second parameter indicates the length of the string that needs to be truncated, when it is a negative integer, the return is empty, the integer number is the length of the Intercept, and when the remaining length of the string is exceeded, until the end of the string - varSUBSTRSTR1 = Str.substr (2);//llohappyworldhello! + varSUBSTRSTR2 = Str.substr (-2);//o! A varSUBSTRSTR3 = Str.substr (2,18);//Llohappyworldhello at varSUBSTRSTR4 = Str.substr ( -2,1);//o - varSUBSTRSTR5 = Str.substr ( -2,-1);//"" - Console.log (SUBSTRSTR1); - Console.log (SUBSTRSTR2); - Console.log (SUBSTRSTR3); - Console.log (SUBSTRSTR4); in Console.log (SUBSTRSTR5); - to //3, substring (numstart,numend) is similar to slice, but when the first argument is a negative integer, the entire string is truncated + varSUBSTRINGSTR1 = str.substring (2);//llohappyworldhello! - varSUBSTRINGSTR2 = str.substring (-2);//hellohappyworldhello! the varSUBSTRINGSTR3 = str.substring (7,2);//Lloha * varSUBSTRINGSTR4 = str.substring (2,7);//Lloha $ Console.log (SUBSTRINGSTR1);Panax Notoginseng Console.log (SUBSTRINGSTR2); - Console.log (SUBSTRINGSTR3); the Console.log (SUBSTRINGSTR4); + A //4, Match method note 1, the match method returns an array containing a match string, 2, the need to write the regular according to different circumstances, 3, the returned array inside the first is always the original string, 4, when the regular expression has a sub-representation (that is, (\s*) parentheses in the content), And for a global match, only find the full match regular expression and return all content, non-global, the return is a plurality of element array, when the regular expression has no sub-expression, and is a global match, the return is an array of multiple elements, if the non-global match, the return is matched to the first element of the array the varREGSTR1 = Str.match (/hello/g);//["Hello", "Hello"] + varREGSTR9 = Str.match (/hello/);//["Hello"] - varREGSTR2 = Str.match (/hello/g);//NULL $ varREGSTR3 = Str.match (/hello (\s*) world/);//["Hellohappyworld", "happy"]//intercept intermediate string $ varREGSTR4 = Str.match (/(\s*) world/g);//["Hellohappyworld"]//intercept the string before the specified character - varREGSTR5 = Str.match (/(\s*) world/);//["Hellohappyworld", "hellohappy"]//intercept the string before the specified character - varREGSTR6 = Str.match (/hello (\s*)/g);//["hellohappyworldhello!"]//intercept the string after the specified character the varREGSTR7 = Str.match (/hello (\s*)/);//["hellohappyworldhello!", happyworld!]//intercept the string after the specified character - varRegStr8 = Str.match (/llo (\s*)/);//["llohappyworldhello!", happyworldhello!]//The string that intercepts the specified characterWuyi Console.log (REGSTR1); the Console.log (REGSTR9); - Console.log (REGSTR2); Wu Console.log (REGSTR3); - Console.log (REGSTR4); About Console.log (REGSTR5); $ Console.log (REGSTR6); - Console.log (REGSTR7); - Console.log (REGSTR8); -</script>
Of course, these methods and use of the above are some simple requirements, when the requirements of the project is more complex to be combined with the actual situation to intercept, but regardless of the method used or implementation of ideas are probably similar, but also to spur a good memory than bad pen, What is the problem or new knowledge or to develop the habit of recording, I hope to share with you garden friends.
4 types of truncated string methods commonly used by JS