2017-09-14 3 views
1

アイブ機能または上記の単語の回出てくる除去するための任意の簡単な方法をしようだから、この単語の配列に基づいて、文章中の特定の単語のすべての発生を置き換え

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 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. (It will cut text such as Привіт). 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."; 
 

 
$(excludeWords).each(function(index, item) { 
 
    var s = new RegExp(item, "gi"); 
 
    sentence = sentence.replace(s, ""); 
 
}); 
 
alert(sentence);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

それをやっしかしループよりも任意のより良い解決策があるあります?コメントに基づいて

もう少し詳細...

これは、単語の一部を削除するべきではありませんでした。それだけであなたはスペースで分割し、言葉だけがフィルタ

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. (It will cut text such as Привіт). 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."; 
 

 
var res = sentence.split(" ").filter(w=>!excludeWords.includes(w.toUpperCase())).join(" "); 
 

 
console.log(res)

に配列している場合は、単純で文字列を置換するかどうかを確認したいの完全な単語

+1

使用JSマップ機能 –

+0

あなたは_'ny'_なった_'any'_その文を持っていると_'ANY'_と一致していません。それは唯一のものですか? –

+0

何かこのようなhttps://stackoverflow.com/a/15604206/1719752 –

答えて

3

あなたはほとんどそこにいます。このトリックは、すべての単語を1つの大きな正規表現に結合して、置換を1回だけ行うことです。 \\bは、部分文字列だけでなく、全体の単語を実際に置き換えることを保証します。

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. (It will cut text such as Привіт). 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."; 
 

 
var re = new RegExp(`\\b(${excludeWords.join('|')})\\b`, 'gi'); 
 
sentence = sentence.replace(re, ""); 
 
console.log(sentence);

これは最終的に文字列の連続したスペースを作成することに注意してください。これらは簡単にreplace(/\s+/g, ' ').trim()で削除できます。

0

を交換する必要がありますたとえば、solutionlutiとなり、soonの両方が配列に含まれるため、代わりに完全な単語を比較する必要があります。

-1

あなたは、配列の値の1つの文字列を作成し、それに正規表現を適用し、再び配列に変換することができますpreg_replace_all("~[\"](.*)[\"]~isuU", $data, $found)

0

を使用することができます。

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 array_to_string = excludeWords.join(' '); 
var s = new RegExp(array_to_string, "gi"); 
var sentence = sentence.replace(s, ""); 
var excludewords_updated = sentence.split(' '); 

これはルーピングなしで行うことができます。

1

単語の置換を取得するための単語境界\bを追加できます。

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"], 
 
    sentence = "The first solution does not work for any UTF-8 alphaben. (It will cut text such as Привіт). 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."; 
 

 
sentence = excludeWords.reduce(function(r, s) { 
 
    return r.replace(new RegExp('\\b' + s + '\\b', "gi"), ""); 
 
}, sentence); 
 

 
console.log(sentence);

関連する問題