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()
- Javascript remove leading and trailing spaces from a string using trim()
- Javascript remove leading and trailing spaces from a string using trimStart() and trimEnd()
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:-
Frequently Asked:
- JavaScript Remove Hyphens from a String
- Javascript: Convert string to number
- Javascript: 4 ways to do case-insensitive comparison
- Remove first and last double quotes from a string in javascript
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:
- Remove character at a specific index from a string in javascript
- Javascript: How to remove text from string
- Javascript: Remove last character of string
- Javascript: Remove first character from a string
I hope this article helped you to delete leading and trailing spaces from a string in javascript. Good Luck !!!