
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Split the sentences by comma and remove surrounding spaces - JavaScript?
Let’s say the following is our string with comma and whitespace −
var sentences = " John , David , Bob , Mike, Carol ";
To split the sentences by comma, use split(). For removing surrounding spaces, use trim().
Example
Following is the code −
var sentences = " John , David , Bob , Mike, Carol "; console.log("The value=" + sentences); var result = sentences.split(",").map(function (value) { return value.trim(); }); console.log("After modifying the value=") console.log(result);
To run the above program, use the following command −
node fileName.js.
Here, my file name is demo235.js.
Output
The output is as follows −
PS C:\Users\Amit\javascript-code> node demo235.js The value= John , David , Bob , Mike, Carol After modifying the value= [ 'John', 'David', 'Bob', 'Mike', 'Carol' ]
- Related Articles
- Remove extra spaces in string JavaScript?
- Split Space Delimited String and Trim Extra Commas and Spaces in JavaScript?
- JavaScript Regex to remove text after a comma and the following word?
- Split String with Comma (,) in Java
- Split string into sentences using regex in PHP
- How to split comma and semicolon separated string into a two-dimensional array in JavaScript ?
- Python program to split a string and join with comma
- How to write a Regular Expression in JavaScript to remove spaces?
- C# Program to split a string on spaces
- MySQL update query to remove spaces?
- MySQL query to remove trailing spaces
- Reversing and preserving spaces in JavaScript
- Replace() with Split() in JavaScript to append 0 if number after comma is a single digit
- Checking smooth sentences in JavaScript
- Trim (Remove leading and trailing spaces) a string in C#

Advertisements