(1) Replace regular matching method
Remove all spaces in the string: str = str.replace(/\s*/g,"");
Remove the spaces at both ends of the string: str = str.replace(/^\s *|\s* KaTeX parse error: Undefined control sequence: \s at position 43:… str.replace(/^\̲s̲*/,""); Remove string... )/g,"");
Example:
var str = " 6 6 “;
var str_1 = str.replace(/\s*/g,”");
console.log(str_1); //66
var str = "6 6 ";
var str_1 = str.replace(/^\s*|\s*$/g,"");
console.log(str_1); //6 6//No output on the left and right sides Space
var str = "6 6 ";
var str_1 = str.replace(/^\s*/,"");
console.log(str_1); //6 6 //The output has spaces on the right and no spaces on the left
var str = "6 6 ";
var str_1 = str.replace(/(\s*$)/g,"");
console.log(str_1); // 6 6//There is a space on the left side of the output and no right side Space
(2) str.trim() method The
trim() method is used to delete the blank characters at both ends of the string and return. The trim method does not affect the original string itself, it returns a new string.
Defect: Only the spaces at both ends of the string can be removed, not the spaces in the middle.
Example:
var str = "6 6 ";
var str_1 = str.trim();
console.log(str_1); //6 6//There are no spaces on the left and right sides of the output
To remove the left space alone, use str.trimLeft(); //var str_1 = str.trimLeft(); To
remove the right space alone, use str.trimRight();//var str_1 = str.trimRight();
(3 ) JQ method:. Trim (str) method. trim (str) method . T R & lt I m ( S T R & lt ) square method .trim () function is used to remove a blank character string ends.
Note: The $.trim() function will remove all line breaks, spaces (including consecutive spaces) and tabs at the beginning and end of the string. If these blank characters are in the middle of the string, they will be retained and will not be removed.
Example:
var str = "6 6 ";
var str_1 = $.trim(str);
console.log(str_1); //6 6//There are no spaces on the left and right sides of the output