2017-09-14 16 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"]; 

所以我試着做一個函數或任何快速的方法來去除上面的話的occurances數組從一句話。不使用任何循環,我如何快速實現這一點。

他們的方式即時通訊做,現在

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>

,但有沒有被任何比循環更好的解決方案?

基於評論多一點細節..

它不應該刪除單詞的一部分。它應該只更換一個完整的字

+1

使用JS地圖功能 –

+0

你有_'any'_這句話,成爲_'ny'_和不匹配_'ANY'_。它是在褻瀆嗎? –

+0

這樣的東西https://stackoverflow.com/a/15604206/1719752 –

回答

3

你快到了。訣竅在於將所有單詞合併成一個大的正則表達式來替換一次。 \\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

你會分裂的空間,只是檢查,如果這個詞是在陣列中的過濾器

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)

如果你簡單地替換字符串一個正則表達式,你就會有一些問題,比如solution最終被luti既是soon在數組中,所以你需要比較完整的字來代替

-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);

相關問題