如何从字符串中提取与禁止单词数组中包含的单词相匹配的单词(并删除任何剩余的空格)?

发布于 2024-10-08 20:52:04 字数 455 浏览 9 评论 0原文

给定一组禁用单词,如何从字符串中提取某些单词。

以下面的伍迪·艾伦名言为例:

Love is the answer, 
but while you are waiting for the answer 
sex raises some pretty good questions

这是要从字符串中提取的单词数组:

var forbidden = new Array("is", "the", "but", "you", 
"are", "for", "the", "some", "pretty");

如何从字符串中提取任何单词,删除任何剩余的空格,以便最终得到以下结果:

Love answer, while waiting answer sex raises good questions

How would you extract certain words from a string, given an array of forbidden words.

Consider the following Woody Allen quote as an example:

Love is the answer, 
but while you are waiting for the answer 
sex raises some pretty good questions

And this is the array of words to extract from the string:

var forbidden = new Array("is", "the", "but", "you", 
"are", "for", "the", "some", "pretty");

How would you extract any words from the string, any remove any left-over whitespace so that you end up with this result:

Love answer, while waiting answer sex raises good questions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2024-10-15 20:52:04
 var quote = "Love is the answer, but while you are waiting for the answer sex raises some pretty good questions";
 var forbidden = new Array("is", "the", "but", "you", "are", "for", "the", "some", "pretty");

 var isForbidden = {};
 var i;

 for (i = 0; i < forbidden.length; i++) {
     isForbidden[forbidden[i]] = true;
 }

 var words = quote.split(" ");
 var sanitaryWords = [];

 for (i = 0; i < words.length; i++) {
     if (!isForbidden[words[i]]) {
          sanitaryWords.push(words[i]);
     }
 }

 alert(sanitaryWords.join(" "));
 var quote = "Love is the answer, but while you are waiting for the answer sex raises some pretty good questions";
 var forbidden = new Array("is", "the", "but", "you", "are", "for", "the", "some", "pretty");

 var isForbidden = {};
 var i;

 for (i = 0; i < forbidden.length; i++) {
     isForbidden[forbidden[i]] = true;
 }

 var words = quote.split(" ");
 var sanitaryWords = [];

 for (i = 0; i < words.length; i++) {
     if (!isForbidden[words[i]]) {
          sanitaryWords.push(words[i]);
     }
 }

 alert(sanitaryWords.join(" "));
别忘他 2024-10-15 20:52:04
var quote = "Love is the answer,\nbut while you are waiting for the answer\nsex raises some pretty good questions";

var forbidden = ["is", "the", "but", "you", "are", "for", "the", "some", "pretty"];

var reg = RegExp('\\b(' + forbidden.join('|') + ')\\b\\s?', 'g');

alert(quote.replace(reg, ''));

尝试一下: http://jsbin.com/isero5/edit

var quote = "Love is the answer,\nbut while you are waiting for the answer\nsex raises some pretty good questions";

var forbidden = ["is", "the", "but", "you", "are", "for", "the", "some", "pretty"];

var reg = RegExp('\\b(' + forbidden.join('|') + ')\\b\\s?', 'g');

alert(quote.replace(reg, ''));

Try it: http://jsbin.com/isero5/edit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文