हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एक स्ट्रिंग और स्ट्रिंग्स की एक सरणी लेता है।
हमारे फ़ंक्शन को एक नया स्ट्रिंग लौटाना चाहिए, जहां सरणी में मौजूद स्ट्रिंग में शब्द की सभी घटनाओं को एक व्हाइटस्पेस से बदल दिया जाता है।
इस समस्या को हल करने के लिए हमारे फ़ंक्शन को String.prototype.replace() विधि का उपयोग करना चाहिए।
उदाहरण
इसके लिए कोड होगा -
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));
आउटपुट
कंसोल में आउटपुट -
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.