
- 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
How to detect and replace all the array elements in a string using RegExp in JavaScript?
Let’s say we have the following string −
var sentence= "My Name is John Smith. I live in UK. My Favourite Subject is JavaScript."
We need to replace the following words in the above sentence with a specific value “Not Available” −
var values = ['John','Smith','UK','JavaScript']
So, the output should be −
My Name is Not Available Not Available. I live in Not Available. My Favourite Subject is Not Available.
You can use regular expression to implement what we discussed above.
Example
Following is the code −
var values = ['John','Smith','UK','JavaScript'] var sentence= "My Name is John Smith. I live in UK. My Favourite Subject is JavaScript." var regularExpression = new RegExp (values.join('|'), 'gim') sentence = sentence.replace(regularExpression, 'Not Available'); console.log(sentence);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo279.js.
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo279.js My Name is Not Available Not Available. I live in Not Available. My Favourite Subject is Not Available.
- Related Articles
- How to replace string using JavaScript RegExp?
- How to replace all dots in a string using JavaScript?
- How to replace all occurrences of a string in JavaScript?
- How to replace elements in array with elements of another array in JavaScript?
- Replace all characters in a string except the ones that exist in an array JavaScript
- How to sum all elements in a nested array? JavaScript
- How to find all elements in a given array except for the first one using JavaScript?
- With JavaScript RegExp how to search a string for text that matches regexp?
- Remove the whitespaces from a string using replace() in JavaScript?
- How to print all the elements of a String array in Kotlin in a single line?
- How to replace all occurrences of a string with another string in Python?
- Reducing array elements to all odds in JavaScript
- How to validate email address using RegExp in JavaScript?
- How to find the sum of all elements of a given array in JavaScript?
- How to create a string by joining the elements of an array in JavaScript?

Advertisements