Reputation:
So I need to remove for example the letter "a" from every word in a string which is an element of array, except the word "an"
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']
So maximum I thought it'll be needed to find an's index, but don't have an idea what to do next.
Upvotes: 0
Views: 31
Reputation: 41
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)
Upvotes: 0
Reputation: 370699
Use a regular expression - match a
with negative lookahead for n
.
const arr = ['sentence example', 'an element of array', 'smth else'];
const output = arr.map(str => str.replace(/a(?!n)/g, ''));
console.log(output);
Upvotes: 1