

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Split a URL in JavaScript after every forward slash?
- Split the left part of a string by a separator string in MySQL?
- JavaScript Insert space after every two letters in string?
- 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?
- Find word character in a string with JavaScript RegExp?
- Squaring every digit of a number using split() in JavaScript
- Java regex program to split a string at every space and punctuation.
- Find non-word character in a string with JavaScript RegExp.
- 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#?
- Java Program to split a string with dot
- How to Split a String with Escaped Delimiters?
- Do I need to use a semicolon after every function in JavaScript?
- Finding shortest word in a string in JavaScript
Advertisements