- 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
Remove characters from a string contained in another string with JavaScript?
Let’s say the following are our two strings −
var originalName = "JOHNDOE"; var removalName = "JOHN"
We need to remove the second string from first. For this, use replace() along with reduce().
Example
const removeCharactersFromAString= (removalName,originalName)=>removalName.split('').reduce((obj,v)=>obj.replace(v,''),originalName); var originalName = "JOHNDOE"; var removalName = "JOHN" console.log(removeCharactersFromAString(removalName,originalName));
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo93.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo93.js DOE
Advertisements