从字符串中除一个特定单词外的每个单词中删除一个字符



所以我需要删除例如字母"a";从作为数组元素的字符串中的每个字中;一个";

var arr = ['sentence example', 'an element of array', 'smth else']
var removedPuncts = []
for (i = 0; i < arr.length; i++) {
if (sentences.indexOf("an")) {
...
}
}
console.log(removedPuncts)
//expected output: ['sentence exmple', 'an element of rry', 'smth else']

所以maximum我认为需要找到一个索引,但不知道下一步该怎么做。

使用正则表达式-将an的负前瞻匹配。

const arr = ['sentence example', 'an element of array', 'smth else'];
const output = arr.map(str => str.replace(/a(?!n)/g, ''));
console.log(output);

const arr = ['sentence example', 'an element of array', 'smth else'];
let out = []
for (i = 0; i < arr.length; i++) {
let text = ""
for (j = 0; j < arr[i].split(" ").length; j++) {
if (arr[i].split(" ")[j] == "an") {
text += "an"
} else {
text += " " + arr[i].split(" ")[j].replaceAll('a', '')
}
}
out.push(text)
}
console.log(out)

最新更新