Remove leading and trailing spaces : Javascript String

Often there is a requirement to remove leading and trailing spaces while working with javascript strings. This article will discuss removing the spaces from the start and end of a string in javascript using simple methods and illustrative examples.

Table of Contents:-

Javascript remove leading and trailing spaces from a string using RegEx()

Regular Expressions are the patterns that are used to match the text within a string.

Example:-

Remove leading and trailing spaces from ” Javascript is a versatile language “

Code:-

let dummyString = '  Javascript is a versatile language  ';
console.log( "*" + dummyString + "*")  // before applying replace() and regEx
dummyString = dummyString.replace(/^\s+|\s+$/g,'')
console.log( "*" + dummyString + "*") // after applying replace() and regEx

Explanation:-

The regular expression used is /^\s+|\s+$/g 

  • / and / mark the beginning and end of the pattern
  • ^\s+ specifies to match one or more spaces at the beginning of the string
  • | means OR
  • \s+$ specifies to match one or more spaces at the end of the string
  • g specifies to replace all occurrences.

Output:-

*  Javascript is a versatile language  *
*Javascript is a versatile language*

Javascript remove leading and trailing spaces from a string using trim()

Javascript’s trim() method removes the whitespace characters like spaces, tabs, no-break spaces, etc., from both ends of the string.

Example:-

Remove leading and trailing spaces from ” Javascript is a versatile language “

Code:-

let dummyString = '  Javascript is a versatile language  ';
console.log( "*" + dummyString + "*") // before applying trim()
dummyString = dummyString.trim();
console.log( "*" + dummyString + "*" ) // after applying trim()

Output:-

*  Javascript is a versatile language  *
*Javascript is a versatile language*

Javascript remove leading and trailing spaces from a string using trimStart() and trimEnd()

Javascript’s trimStart() method removes the whitespace from the start of the string. Method trimLeft() is an alias of trimStart().

Javascript’s trimEnd() method removes the whitespace from the end of the string. Method trimRight() is an alias of trimEnd().

Example:-

Remove leading and trailing spaces from ” Javascript is a versatile language “

Code:-

let dummyString = '  Javascript is a versatile language  '
console.log( "*" + dummyString + "*") //before applying trimStart() and trimEnd()
dummyString = dummyString.trimStart().trimEnd(); 
console.log( "*" + dummyString + "*" ) // after applying trimStart() and trimEnd()

OR

let dummyString = '  Javascript is a versatile language  '
console.log( "*" + dummyString + "*") // before applying trimLeft() and trimRight()
dummyString = dummyString.trimLeft().trimRight(); 
console.log( "*" + dummyString + "*" ) // after applying trimLeft() and trimRight()

Output:-

*  Javascript is a versatile language  *
*Javascript is a versatile language*

Read More:

I hope this article helped you to delete leading and trailing spaces from a string in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top