![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we separate the special characters in JavaScript?
To separate the special character, use the concept of match() with Regular Expression. The syntax is as follows −
yourStringName.flatMap(anyVariableName => yourVariableName.match(/\w+|\W+/g));
Let’s say, the following is our array with special characters in between values −
var allNames = ['John-Smith', 'David', 'Carol%Taylor'];
Let’s now see how to separate text with special characters. Following is the code −
Example
var allNames = ['John-Smith', 'David', 'Carol%Taylor']; var output = allNames.flatMap(obj => obj.match(/\w+|\W+/g)); console.log(output);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo32.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo32.js [ 'John', '-', 'Smith', 'David', 'Carol', '%', 'Taylor' ]
Advertisements