JavaScript trim() method
Belongs to JavaScript String object
Definition and usage
- The trim() method is used to remove the leading and trailing spaces of the string
- trim() method does not change the original string
grammar
string.trim() // 参数无
return value
Code example
var str = ' Milk595 '
str.trim()
If your browser does not support the trim() method, you can use regular expressions to achieve:
function myTrim(x) {
return x.replace(/^\s+|\s+$/gm,'');
}
function myFunction() {
var str = myTrim(' Milk595 ')
console.log(str);
}
myFunction()