문자열과 문자열 배열을 취하는 JavaScript 함수를 작성해야 합니다.
우리의 함수는 배열에 있는 문자열의 모든 단어가 공백으로 대체되는 새 문자열을 반환해야 합니다.
우리 함수는 이 문제를 해결하기 위해 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.