
- 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
Replace all characters in a string except the ones that exist in an array JavaScript
Let’s say, we have to write a function −
replaceChar(str, arr, [char])
Now, replace all characters of string str that are not present in array of strings arr with the optional argument char. If char is not provided, replace them with ‘*’.
Let’s write the code for this function.
The full code will be −
Example
const arr = ['a', 'e', 'i', 'o', 'u']; const text = 'I looked for Mary and Samantha at the bus station.'; const replaceChar = (str, arr, char = '*') => { const replacedString = str.split("").map(word => { return arr.includes(word) ? word : char; }).join(""); return replacedString; }; console.log(replaceChar(text, arr));
Output
The console output of this code will be −
***oo*e***o***a***a****a*a***a*a****e**u****a*io**
- Related Articles
- String replace multiple characters with an asterisk in JavaScript
- How to replace characters except last with a mask character in JavaScript?
- Python program to replace all Characters of a List except the given character
- C++ Program to Remove all Characters in a String Except Alphabets
- Program to replace all the characters in of a file with '#' except a particular word in Java
- Replace characters in a string in Arduino
- Replace tab characters by a fixed tabsize in a string array in Numpy
- How to detect and replace all the array elements in a string using RegExp in JavaScript?
- How to replace all dots in a string using JavaScript?
- How to replace all occurrences of a string in JavaScript?
- How to Replace characters in a Golang string?
- Removing all non-alphabetic characters from a string in JavaScript
- Mapping unique characters of string to an array - JavaScript
- How to replace all the special characters following another character – JavaScript?
- Reverse the words in the string that have an odd number of characters in JavaScript

Advertisements