我想将用户提供的文本拆分为单个字符,然后对其进行编辑。
我使用正则表达式将整个句子分成单词,然后分成字符,但最后我得到了数组数组:/
代码(检查控制台):
function showLetters() {
const simpleText = 'Hello my friends';
let words = simpleText.split(/\W+/),
letters = words.map(word => {
return word.match(/\S/g).join();
});
console.log(letters);
}
showLetters();
我想知道如何在最终数组中找到这些字母。
我是新手,请求帮助
最佳答案
这里不需要正则表达式,我不认为,如果我理解正确的话,这就是你需要的:
function showLetters() {
const simpleText = "Hello my friends";
let letters = simpleText.split("").filter(letter => letter !== " ");
console.log(letters);
}
showLetters();
如果您希望“空白”保留为数组中的实体,只需删除 .filter 部分即可。
关于javascript - 如何将句子拆分为字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60705601/