如何从数组中获得一个对象,只有一个字,不包含任何特殊字符?



如何从wordnet中获得只有1个单词的列表,而不包含特殊字符?我试着让它像:

const wordnet = require('wordnet')
await wordnet.init();
let results = await wordnet.list();
const words = results.filter(m=> m.includes("raine"))
//array = ["capital of the ukraine", "rainer maria rilke", "cross of lorraine", "straight-grained", "quiche lorraine", "coarse-grained", "featherbrained", "pebble-grained", "scatterbrained",  "toilet-trained", "unrestrainedly", "animal trainer", "lorraine cross", "trained worker", "close-grained", "cross-grained"]
message.channel.send(`${words}`)
//expected output: ["featherbrained", "scatterbrained", "unrestrainedly"]

我知道这可以使用RegExp,但我不知道怎么做。我需要帮助。

您可以先检查字母字符

const
array = ["capital of the ukraine", "rainer maria rilke", "cross of lorraine", "straight-grained", "quiche lorraine", "coarse-grained", "featherbrained", "pebble-grained", "scatterbrained", "toilet-trained", "unrestrainedly", "animal trainer", "lorraine cross", "trained worker", "close-grained", "cross-grained"],
words = array.filter(m => /^[a-z]+$/i.test(m) && m.includes("raine"));
console.log(words);

另一种方法是使用字符串并从中构建正则表达式。

const
array = ["capital of the ukraine", "rainer maria rilke", "cross of lorraine", "straight-grained", "quiche lorraine", "coarse-grained", "featherbrained", "pebble-grained", "scatterbrained", "toilet-trained", "unrestrainedly", "animal trainer", "lorraine cross", "trained worker", "close-grained", "cross-grained"],
word = 'raine',
wordR = new RegExp(`^[a-z]*${word}[a-z]*$`, 'i'),
words = array.filter(RegExp.prototype.test, wordR);
console.log(words);

相关内容

  • 没有找到相关文章

最新更新