[JS basics] trim() method

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

Insert picture description here

Code example

var str = '     Milk595      '
str.trim()

Insert picture description here

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()

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43352901/article/details/108397530