JavaScript — Trim white space

By Rares Vernica

Here is a way to trim the white space from a string in Javascript:

replace(/^\s+|\s+$/g, ”)

By removing |\s+$ it will do a left trim only, and by removing ^\s+| it will do a right trim only (g is no longer necessary).

Leave a Reply

You must be logged in to post a comment.