
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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
- Related Articles
- JavaScript Remove non-duplicate characters from string
- JavaScript - Remove first n characters from string
- Remove all characters of first string from second JavaScript
- How to Remove Characters from a String in Arduino?
- Write a Regular Expression to remove all special characters from a JavaScript String?
- How to remove certain characters from a string in C++?
- How to remove specific characters from a string in Python?
- C# Program to remove duplicate characters from String
- Remove newline, space and tab characters from a string in Java
- Program to remove duplicate characters from a given string in Python
- How to remove “,” from a string in JavaScript
- How to remove characters except digits from string in Python?
- PHP program to remove non-alphanumeric characters from string
- Remove all whitespaces from string - JavaScript
- How to remove all non-alphanumeric characters from a string in MySQL?

Advertisements