简体   繁体   English

从字符串中删除字符串中的每个单词

[英]Remove each word in a string from a string

So I'm in a situation where I want to remove dynamically generated text from another piece of text.. 所以我要从另一段文本中删除动态生成的文本。

  var string1 = "Hello, my name is irrelevant";
  var string2 = "Hello irrelevant, my name is blank";

So what I want is that when string1 is "removed" from string2 the result is blank because string1 had all the same words as string2 but even though they were in a different pattern they still get removed. 所以我想要的是,当从string2 “删除” string1时,结果为blank因为string1string2具有相同的单词,但是即使它们处于不同的模式,它们仍然会被删除。

Is that possible? 那可能吗? If I have a very large string would it be slow? 如果我的弦很大,会很慢吗?

Generate a regular expression from string1 and use that to eliminate words from string2 : string1生成一个正则表达式,并使用它来消除string2单词:

var reg = new RegExp('\\b('+string1.match(/\w+/g).join('|')+')\\b','gi');
var unmatched_words = string2.replace(reg,'').match(/\w+/g);
alert(unmatched_words);

This should produce an array of every word that appears in string2 but not in string1 . 这应该产生出现在string2但不是出现在string1的每个单词的数组。

EDIT: As Guffa pointed out, the regular expression will also match partial words unless it is bookended with word boundary match strings ( \\b ). 编辑:正如Guffa所指出的,除非将正则表达式与单词边界匹配字符串( \\b )附加在一起,否则它也将匹配部分单词。 I've updated the answer to include this change. 我已经更新了答案以包含此更改。

 var string1 = "Hello, my name is irrelevant"; var string2 = "Hello irrelevant, my name is blank"; var reg = new RegExp('\\\\b('+string1.match(/\\w+/g).join('|')+')\\\\b','gi'); var unmatched_words = string2.replace(reg,'').match(/\\w+/g); alert(unmatched_words); 

You can make a map of the words from the first string, then loop through the words in the second string and check which ones are not in the map. 您可以对第一个字符串中的单词进行映射,然后遍历第二个字符串中的单词并检查哪些单词不在映射中。 Example: 例:

 var string1 = "Hello, my name is irrelevant"; var string2 = "Hello irrelevant, my name is blank"; var map = {}; var words1 = string1.replace(/,/g, '').split(' '); for (var i = 0; i < words1.length; i++) { map[words1[i]] = 1; } var result = []; var words2 = string2.replace(/,/g, '').split(' '); for (var i = 0; i < words2.length; i++) { if (!(words2[i] in map)) { result.push(words2[i]); } } document.write(JSON.stringify(result)); 

This will be an O(n) solution, ie the performance will be linear (or close to linear) to the length of the strings, so that will perform very well for large strings. 这将是一个O(n)解决方案,即,性能将与弦的长度成线性关系(或接近于线性),因此对于大型弦而言效果将非常好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM