【发布时间】:2020-11-05 13:37:55
【问题描述】:
我有一个非常基本的问题,而且我是字符串新手。 在我的代码中,我有大量的单词,其中大部分包含我从用户当前网页获取的字母(这是一个 chrome 扩展)。有时我会得到想要从该字符串中删除的数字、逗号和分号。如何完全删除它们?
我已经在我的 Javascript 中尝试过这个 -
// p is a large string and words is the array of each word from that string formed by separating each form by ' '
let words = p.split(' ');
words.forEach(word => {
console.log(word)
let arr = word.replace('[,]+', '')
console.log(arr)
})
【问题讨论】:
-
先替换再拆分成单词不是更方便吗?
标签: javascript regex string replace