Does it include

Includes (includes or not)

str.includes("xxx")
Copy the code

StartsWith (whether included at the beginning)

// start with XXX
str.startsWith('xxx')
Copy the code

EndsWith (whether the end contains)

// Whether the end is.txt
str.endsWith('.txt')
Copy the code

Search index

Indexof (Start from scratch)

/* character/string position in STR (index value), if not found returns -1 argument 2: search right from index 4 (default 0 if not written) */Str.indexof (character/string,4)
Copy the code

LastIndexof (find from the end)

// Work from the back to the frontStr.lastindexof (character/string)Copy the code

replace

Replace (Replace some characters)

// Replace some of the original characters with the replacement content, the original string remains unchanged
letStr1 = str.replace(part of the original character, what to replace)// Return the replaced string (replace only the first matched string)
let str1 = str.replace(/ Regular expression /, the content of the replacement);// Return the replaced string (replace all matches)
let str1 = str.replace(/ regular expression /g, the content of the replacement);// Return the replaced string (m: for each match)
let str1 = str.replace(/ regular expression /g.m= > m.toLowerCase());
Copy the code

segmentation

Split (split into arrays)

// Delimit the string into the array arr by a space,4 means take the first four.
let arr = str.split("".4)
Copy the code

Regular match

Match (regular match)

// Match a single, return the first matched
str.match(/ Regular expression /);
// add g to indicate all matches and return an array
// String matchAll(/ regular expression /);
str.match(/ regular expression /g);
// I: ignores case and returns the first matched
str.match(/ regular expression/I);
// gi: ignore case and match all
str.match(/ regular expression /gi);
Copy the code

matchAll

// Return an array
str.matchAll(/ Regular expression /);
Copy the code

Case conversion

ToLowerCase (all lowercase)

let str1 = str.toLowerCase()
Copy the code

ToUpperCase (all uppercase)

let str1 = str.toUpperCase()
Copy the code

Go to the space

TrimStart (go ahead space)

let str1 = str.trimStart()
Copy the code

TrimEnd (go after space)

let str1 = str.trimEnd()
Copy the code

Trim (remove the Spaces)

let str1 = str.trim()
Copy the code

completion

PadStart (beginning completion)

'xxx'.padStart(7.'ab') // 'ababxxx'
'xxx'.padStart(6.'ab') // 'abaxxx'
'xxx'.padStart(2.'ab') // 'xxx'
'x'.padStart(4) // 'x'(no argument two, use space to complete)
Copy the code

PadEnd (end completion)

'xxx'.padEnd(7.'ab') // 'xxxabab'
'xxx'.padEnd(6.'ab') // 'xxxaba'
'xxx'.padEnd(2.'ab') // 'xxx'
'x'.padEnd(4) // 'x '(no argument two, use space to complete)
Copy the code

The interception

Slice

// Intercepts a string in positions 3 to 5
let str1 = str.slice(3.6)
Copy the code

Substr (truncated characters)

// Cut the string from position 3 to 8,6 means 6 (if 6 is not written, cut to the last)
let str1 = str.substr(3.6)
Copy the code