
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
Separate String into Substrings in JavaScript
Let’s say we have the following string with special character sequence −
var fullName=" John <----> Smith ";
To separate the above string into substring, use regex and then split(). The syntax is as follows −
var anyVariableName=(/\s*<---->\s*/g); var anyVariableName=yourVariableName.trim().split(yourVariableName);
Following is the complete JavaScript code −
Example
var fullName=" John <----> Smith "; console.log("The Value="+fullName); var regularExpression=(/\s*<---->\s*/g); var seprateName=fullName.trim().split(regularExpression); console.log(seprateName);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo39.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo39.js The Value= John <----> Smith [ 'John', 'Smith' ]
Advertisements