This is the 24th day of my participation in the August More Text Challenge

Two, interception splicing

2.1 the concat

The concat() method merges one or more strings with the original string concatenation to form a new string and returns it.

str.concat(str2, [, ...strN])

  • @params: the string that needs to be connected to STR.
  • @return: a new string containing the connection string provided by the argument.
  • Whether to change the original string: don’t change
let hello = 'Hello, '
console.log(hello.concat('Kevin'.'. Have a nice day.'))
// Hello, Kevin. Have a nice day.

let greetList = ['Hello'.' '.'Venkat'.'! ']
console.log(' '.concat(... greetList))// 'Hello Venkat! '
console.log(' '.concat({}))    // [object Object]
console.log(' '.concat([]))    / /"
console.log(' '.concat(null))  // 'null'
console.log(' '.concat(true))  // 'true'
console.log(' '.concat(4.5))  / / '45'
Copy the code

2.2 slice

The slice() method extracts a portion of a string and returns a new string, leaving the original string unchanged.

str.slice(beginIndex[, endIndex])

  • @params:
    • beginIndex: Extracts characters from the original string starting at the index (base 0). If the value is negative, it will be treated asstrLength + beginIndexLook at, herestrLengthIs the length of the string (for example, if beginIndex is -3 then consider:strLength - 3);
    • endIndex: optional. The extraction string ends at the index (base 0). If omitted, slice() extracts all the way to the end of the string. If the parameter is negative, it is treated asstrLength + endIndex, where strLength is the length of the string (for example, if endIndex is -3,strLength - 3).
  • @return: returns a new string extracted from the original string.
  • Whether to change the original string: don’t change
let str1 = 'The morning is upon us.'.// The length of str1 is 23.
    str2 = str1.slice(1.8),
    str3 = str1.slice(4, -2),
    str4 = str1.slice(12),
    str5 = str1.slice(30);
console.log(str2); // Output: he morn
console.log(str3); // Output: morning is upon u
console.log(str4); // Output: is upon us.
console.log(str5); // output: ""
Copy the code

2.3 substr

The substr() method returns a string of characters from the specified position up to the specified number of characters. (Use substring() instead if possible.)

str.substr(start[, length])

  • @params:
    • Start: indicates the position to start extracting characters. If it is negative, it is treated as strLength + start, where strLength is the length of the string (for example, if start is -3, it is treated as strLength + (-3)).
    • Length: Indicates the number of characters to be extracted.
  • @return: characters in a string starting at a specified position and counting characters.
  • Whether to change the original string: don’t change
let str = 'abcdefghij';

console.log('(1, 2) :    + str.substr(1.2));   / / (1, 2) : the BC
console.log('(3, 2) :   + str.substr(-3.2));  / / (3, 2) : hi
console.log('(3) :     + str.substr(-3));    // (-3): hij
console.log('(1) :      + str.substr(1));     // (1): bcdefghij
console.log('(20, 2) : + str.substr(-20.2)); // (-20, 2): ab
console.log('(20, 2) :  + str.substr(20.2));  / / (20, 2) :
Copy the code

2.4 the substring

The substring() method returns a subset of a string from the start index to the end index, or from the start index to the end of the string.

str.substring(indexStart[, indexEnd])

  • @params:
    • IndexStart: The index of the first character to be truncated as the first letter of the returned string.
    • IndexEnd: Optional, an integer between 0 and the length of the string. Characters indexed by this number are not included in the truncated string.
  • @return: a new string containing the specified part of the given string.
  • Whether to change the original string: don’t change
let str = 'Mozilla';

/ / output 'Moz'
console.log(str.substring(0.3));
console.log(str.substring(3.0));
console.log(str.substring(3, -3));
console.log(str.substring(3.NaN));
console.log(str.substring(-2.3));
console.log(str.substring(NaN.3));

/ / output 'lla'
console.log(str.substring(4.7));
console.log(str.substring(7.4));

/ / output"
console.log(str.substring(4.4));

/ / output 'Mozill
console.log(str.substring(0.6));

/ / output 'Mozilla'
console.log(str.substring(0.7));
console.log(str.substring(0.10));
Copy the code

The difference between slice, substr and substring

  • Both receive two arguments
  • Slice and Substring receive start and end positions (excluding end positions)
  • Substr receives the starting position and the length of the string to be returned
let test = 'hello world';

console.log(test.slice(4.7));  //o w
console.log(test.substring(4.7));  //o w
console.log(test.substr(4.7));  //o world
Copy the code

⚠️ Note: substring starts with the smaller of the two arguments and ends with the larger argument

console.log(test.substring(7.4));  //o w
Copy the code

When the received parameter is negative:

  • Slice adds the length of its string to the corresponding negative number, and takes the result as an argument.
  • Substring simply converts all negative arguments to 0.
  • Substr simply takes the result of the first argument plus the length of the string as the first argument.
let test = 'hello world';

console.log(test.slice(-3));         //rld
console.log(test.substring(-3));     //hello world
console.log(test.substr(-3));        //rld
console.log(test.slice(3, -4));       //lo w
console.log(test.substring(3, -4));   //hel
console.log(test.substr(3, -4));      // An empty string
Copy the code

2.5 the replace

The replace() method returns a new string that replaces some or all of the pattern matches with the replacement value. The pattern can be a string or a regular expression, and the replacement value can be a string or a callback function that is called on every match. If pattern is a string, only the first match is replaced.

str.replace(regexp|substr, newSubStr|function)

  • @params:
    • Regexp (Pattern) : A regexp object or its literal. What the re matches is replaced by the return value of the second argument. When performing global search and replace, the regular expression must contain the G flag.
    • Substr (pattern): A string to be replaced by newSubStr. It is treated as an entire string, not as a regular expression. Only the first match will be replaced.
    • NewSubStr (replacement): A string used to replace the matching part of the first argument in the original string. Special variable names can be interpolated into the string.
    • Function (replacement): A function that creates a new substring and returns a value that replaces the result of the first argument.
  • @return: A string that partially or completely matches a new string replaced by an alternate pattern.
  • Whether to change the original string: don’t change

When replacing () is passed in as a string, only the first substring is replaced. To replace all substrings, you pass in a regular expression and specify the global (g) flag.

let text = 'cat , bat , sat , fat';
let result = text.replace('at'.'ond');
console.log(result); // =>'cont , bat , sat , fat'

result = text.replace(/at/g.'ond');
console.log(result); //=>'cont , bont , sont , font'
Copy the code

When the second argument is a function, the return value of the function is used as the replacement string. Just as the second argument is a string, if the first argument is a regular expression and there is a global match, the method of this function will be called multiple times, with each match being called.

function replacer(match, p1, p2, p3, offset, string) {
  // p1 is nondigits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(The '-');
}
let newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);  // abc - 12345 - #$*%
Copy the code

2.6 replaceAll

The replaceAll() method returns a new string, all parts of which meet pattern are replaced by replacement. Pattern can be a string or a RegExp, replacement can be a string or a function that is called each time a match is made.

const newStr = str.replaceAll(regexp|substr, newSubstr|function)

  • @params:
  • @return: a new string that all matches the replacement pattern.
  • Whether to change the original string: don’t change
'aabbcc'.replaceAll('b'.'. ');
// 'aa.. cc'
Copy the code

2.7 repeat

Repeat () constructs and returns a new string that contains copies of a specified number of strings to be concatenated together.

str.repeat(count)

  • @params: An integer between 0 and +Infinity. Represents how many times the original string is repeated in the newly constructed string.
  • @return: a new string containing a specified number of copies of the specified string.
  • Whether to change the original string: don’t change
'abc'.repeat(-1)  // RangeError: repeat count must be positive and less than inifinity
'abc'.repeat(0)   / /"
'abc'.repeat(1)   // 'abc'
'abc'.repeat(2)   // 'abcabc'
'abc'.repeat(3.5) // the 'abcabcabc' argument count will be automatically converted to an integer.
'abc'.repeat(1/0) // RangeError: repeat count must be positive and less than inifinity
Copy the code