
- 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 occurrence of specific words in a sentence based on an array of words in JavaScript
We are required to write a JavaScript function that takes a string and an array of strings.
Our function should return a new string, where all the occurrences of the word in the string that are present in the array are replaced by a whitespace.
Our function should use the String.prototype.replace() method to solve this problem.
Example
The code for this will be −
var excludeWords = ["A", "ABOUT", "ABOVE", "ACROSS", "ALL", "ALONG", "AM", "AN", "AND", "ANY", "ASK", "AT", "AWAY", "CAN", "DID", "DIDN'T", "DO", "DON'T", "FOR", "FROM", "HAD", "HAS", "HER", "HIS", "IN", "INTO", "IS", "IT", "NONE", "NOT", "OF", "ON", "One", "OUT", "SO", "SOME", "THAT", "THE", "THEIR", "THERE", "THEY", "THESE", "THIS", "TO", "TWIT", "WAS", "WERE", "WEREN'T", "WHICH", "WILL", "WITH", "WHAT", "WHEN", "WHY"]; var sentence = "The first solution does not work for any UTF-8 alphaben. I have managed to create function which do not use RegExp and use good UTF-8 support in JavaScript engine. The idea is simple if symbol is equal in uppercase and lowercase it is special character. The only exception is made for whitespace."; const removeExcludedWords = (str, words) => { let sentence = ''; const regex = new RegExp(`\b(${words.join('|')})\b`, 'gi'); sentence = str.replace(regex, ""); return sentence; }; console.log(removeExcludedWords(sentence, excludeWords));
Output
The output in the console −
first solution does work UTF-8 alphaben. I have managed create function use RegExp use good UTF-8 support JavaScript engine. idea simple if symbol equal uppercase lowercase special character. only exception made whitespace.
- Related Articles
- Order an array of words based on another array of words JavaScript
- Constructing a sentence based on array of words and punctuations using JavaScript
- Reverse all the words of sentence JavaScript
- Counting number of words in a sentence in JavaScript
- Replace words of a string - JavaScript
- Python - Generate all possible permutations of words in a Sentence
- How to find specific words in an array with JavaScript?
- Print all words occurring in a sentence exactly K times
- Arranging words by their length in a sentence in JavaScript
- Rearrange Words in a Sentence in C++
- Finding n most frequent words from a sentence in JavaScript
- Generate all combinations of supplied words in JavaScript
- Count words in a sentence in Python program
- Count palindrome words in a sentence in C++
- Sorting string of words based on the number present in each word using JavaScript

Advertisements