【问题标题】:Javascript: find english word in string [closed]Javascript:在字符串中查找英文单词[关闭]
【发布时间】:2016-04-21 06:37:43
【问题描述】:

我想在一个字符串中找到任何英文单词(最少 4 个字母)。

Eg: hello123fdwelcome => ["hello", "welcome"]; 

你能给我建议任何解决方案或javascript库来匹配英文单词吗?

谢谢

【问题讨论】:

  • 预期输出是什么?
  • @thefourtheye 输入为:hello123fdwelcome;输出:[“你好”,“欢迎”]
  • 这不是问题。这是一个要求不高的要求列表。
  • 我猜你会有一个包含所有要比较的有效单词的来源(例如一个表格),对吧?
  • @anna 我需要一个包含源代码的库

标签: javascript node.js dictionary


【解决方案1】:

你可以使用这个单词列表https://github.com/dwyl/english-words

var input = "hello123fdwelcome";
var fs = require('fs');
fs.readFile("words.txt", function(words) {
   var words = words.toString().split('\n').filter(function(word) {
       return word.length >= 4;
   });
   var output = [];
   words.forEach(word) {
       if (input.match(word)) {
           output.push(word);
       }
   });
   console.log(output);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2012-10-30
    相关资源
    最近更新 更多