Trim whitespace from either end


August 24th, 2023



trimString.js

Gets rid of whitespace on either side of a string and returns a new string. For example, trimString(" abc ") will result in "abc".

// https://stackoverflow.com/a/3084733/5071723
function trimString(str) {
  return str.replace(/^\s+|\s+$/g, "");
}

This site is open source. Improve this page »