Use str.trim() instead of str.replace(/^\s+|\s+$/g, '')

It behaves exactly same

References:
  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim