This is the 25th day of my participation in the August Challenge
Modify the conversion
3.1 the split
The split() method splits a String into an array of substrings using the specified delimiter String, with a specified split String determining the location of each split.
str.split([separator[, limit]])
@params
:- Separator: Specifies a string representing the point at which each split should occur. Separator can be a string or regular expression. If the delimiter is an empty string, the array of each character in the original STR string is returned.
- Limit (Optional) : An integer that limits the number of fragments returned.
@return
: Returns an Array of the source string delimited by the occurrence position of the delimiter.Whether to change the original string
: don’t change
let color = 'red,blue,yellow,black';
let color1 = color.split(', '); // =>['red','blue','yellow','black']
let color2 = color.split(', '.2); // =>['red','blue']
let color3 = color.split(/ [, ^ \] + /); // =>['', ',', ',', ',', '']
Copy the code
The last call to split shows two blanks before and after because the delimiter specified by the regular expression appears at the beginning and end of the string.
3.2 toLowerCase
ToLowerCase () converts the string value that called the method toLowerCase and returns.
3.3 toUpperCase
The toUpperCase() method converts the string calling the method toUpperCase and returns it (cast if the value calling the method is not a string).
Blank characters
4.1 the trim
The trim() method removes whitespace characters from both ends of a string. Whitespace characters in this context are all whitespace characters (space, TAB, no-break space, etc.) and all line terminator characters (such as LF, CR, etc.).
str.trim()
@params
:@return
: a new string representing whitespace removed from both ends of the calling string.Whether to change the original string
: don’t change
let orig = ' foo ';
console.log(orig.trim()); // 'foo'
let orig = 'foo ';
console.log(orig.trim()); // 'foo'
Copy the code
4.2 trimStart
The trimStart() method removes whitespace from the beginning of the string. TrimLeft () is an alias for this method.
str.trimStart();
@params
:@return
: a new string representing the call string with whitespace removed from its beginning (left).Whether to change the original string
: don’t change
let str = " foo ";
console.log(str.length); / / 8
str = str.trimStart() TrimLeft () = str.trimleft ();
console.log(str.length); / / 5
console.log(str); // "foo "
Copy the code
4.3 trimRight
The trimEnd() method removes whitespace characters from the end of a string. TrimRight () is an alias for this method.
str.trimEnd();
@params
:@return
: a new string that removes whitespace from the end (right) of the calling string.Whether to change the original string
: don’t change
let str = " foo ";
console.log(str.length); / / 8
str = str.trimRight(); // or write STR = str.trimend ();
console.log(str.length); / / 6
console.log(str); // ' foo'
Copy the code
5. Regular expression
5.1 the match
The match() method retrieves the result of a string matching a regular expression.
str.match(regexp)
@params
: a regular expression object.@return
:- If the G flag is used, all results that match the full regular expression are returned, but the capture group is not returned.
- If the G flag is not used, only the first full match and its associated capture group (Array) are returned. In this case, the returned item will have the additional properties described below.
Whether to change the original string
: don’t change
const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const regex = /[A-Z]/g;
const found = paragraph.match(regex);
console.log(found);
// expected output: Array ["T", "I"]
Copy the code
5.2 matchAll
The matchAll() method returns an iterator containing the results of all matching regular expressions and the grouped capture groups.
str.matchAll(regexp)
@params
: a regular expression object.@return
: an iterator.Whether to change the original string
: don’t change
const regexp = /t(e)(st(\d?) )/g;
const str = 'test1test2';
const array = [...str.matchAll(regexp)];
console.log(array[0]);
// expected output: Array ["test1", "e", "st1", "1"]
console.log(array[1]);
// expected output: Array ["test2", "e", "st2", "2"]
Copy the code
5.3 the search
The search() method performs a search match between a regular expression and a String.
str.search(regexp)
@params
: a regular expression object.@return
: If the match is successful, search() returns the index of the first regular expression match in the string; Otherwise, -1 is returned.Whether to change the original string
: don’t change
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy? ';
// any character that is not a word character or whitespace
const regex = /[^\w\s]/g;
console.log(paragraph.search(regex));
// expected output: 43
console.log(paragraph[paragraph.search(regex)]);
// expected output: "."
Copy the code