var a = 'The farthest distance in the world is not the ends of the world';
1. Get the position through characters or get the character through positions:
//Specify the return character((1)); (str[1]); //Return character encoding at the specified position((1)); //Return to the string position(("o"));//Return -1 if not present(("o"));
The execution results in the browser are as follows:
If there is a variable now:
var a = 'The farthest distance in the world is not the ends of the world';
var b = 'Farest distance';
Now if you want to dynamically get the string before variable b and the string after variable, you can do this
1. Use slice to get the previous string;
2. Get the length of variable b, add the length of b after the initial position of b, and intercept the length of a from the tail of b (of course, when the length can be omitted!);
2. Splicing strings:
There are generally two types:
a. Use "+" directly;
function
3. Cutting method
((3,7));//Start, end (there is a start, an end)((3,7));//Start, end (there is a start, an end)((3,7));//Start, length (with start, no end, length) ((-3,-2));//The first negative value is added to the length, and the second negative value is added to the length((3,-1));//The first negative value is converted to 0, the second negative value is converted to 0, if the start is greater than the end, then the adjustment is made((-3,-1));//The first negative value is added to the length, and the second negative value is converted to 0
Here is an example:
var type can be dir/file
if (type !== 'dir' && ('.') !== -1) {//file basename=(0,('.')); extension=(('.')); } else {//dir basename=name; extension=false; } //Chinese
4. Remove the spaces before and after
var strValue = " hello world! "; var trimedStrValue = (); (strValue); (trimedStrValue);
5. Case conversion method
var strLowUp = "HELLO world!"; (()); (()); (strLowUp);
6. Methods related to patterns, the caller is a string, and the parameters are a pattern (regular object or regular expression or string)
1. Matching method is essentially the same as the exec() method that calls RegExp (the caller is a regular expression or a regular object, and the parameters are strings)
//Return the array object, the first item is the matching string, and the other items are the matching strings to the capture group
//Return object has both index and input properties. Index is the index matching the string, and input is the string str that matches the string.
2. Query/Search Method
//The return value is the index of the matching character. If it is not queried, it will return -1
The search() method is used to retrieve the specified substring in a string, or to retrieve the substring that matches a regular expression.
3. Replace method, two parameters, replace parameter one with parameter two, parameter one is a string or regular expression, parameter two is a string or function
a. //Regular
b. String
c. Function
//If the second parameter is a function//The parameters received by the function are: the matched substring, The first capture group matches the substring、 The substring matched by the second capture group...、Index of pattern matching、Original string var repStr = (/at/g,function(match,index,input){ (match);//at (index);// (input);//cat,bat,sat,fat return "an"; }); (repStr);//can,ban,san,fan
4. Segmentation method, divide the string into multiple substrings according to the specified separator.
//The return value is an array that stores multiple substringsvar str = "red,blue,green,yellow"; var strArray = (","); var strArray = (/[^\,]+/);//Match all characters that are not comma as delimiters(strArray);//["red", "blue", "green", "yellow"] //The second parameter is used to control the length of the arrayvar strArray = (",",2); (strArray);//["red", "blue", "green", "yellow"]
7. Comparison method, the return values are 1(>0), 0, -1(<0)
var strValue = "yellow"; (("brick"));//>0 (("yellow"));//0 (("zoo"));//<0
The above is the detailed explanation and integration of js string using interception and splicing introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!