
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
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' ]
Advertisements