
- 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
Possible to split a string with separator after every word in JavaScript
To split a string with separator after every word, the syntax is as follows −
var anyVariableName=yourVariableName.split('parameter').filter(value=>value)
Let’s say, the following is our string with separator −
var sentence="-My-Name-is-John-Smith-I-live-in-US";
Now, split the string with separator as in the below code.
Example
var sentence="-My-Name-is-John-Smith-I-live-in-US"; console.log("The value="+sentence); var result=sentence.split('-').filter(value=>value) console.log("After split()="); console.log(result);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo63.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo63.js The value=-My-Name-is-John-Smith-I-live-in-US After split()= [ 'My', 'Name', 'is', 'John', 'Smith', 'I', 'live', 'in', 'US' ]
- Related Articles
- Split a URL in JavaScript after every forward slash?
- Split the left part of a string by a separator string in MySQL?
- Convert string with separator to array of objects in JavaScript
- How to add a character to the beginning of every word in a string in JavaScript?
- JavaScript Insert space after every two letters in string?
- Find word character in a string with JavaScript RegExp?
- Find non-word character in a string with JavaScript RegExp
- Squaring every digit of a number using split() in JavaScript
- MySQL query to split the string “Learn With Ease” and return the last word?
- Java regex program to split a string at every space and punctuation.
- How to update a value with substring of current value by removing the separator and numbers after a separator in MySQL?
- How to split a string with a string delimiter in C#?
- Replace() with Split() in JavaScript to append 0 if number after comma is a single digit
- Insert a character after every n characters in JavaScript
- Finding shortest word in a string in JavaScript

Advertisements