hello everyone I should add the character " ' " at the end of each word contained in a string, where the final letter is accented. for instance: Niccòlò Niccòlò -> Niccòlò' Niccòlò'
let stringa= "nicolò à";
let apice ="\'";
let stringa2 = stringa.split(' ');
let stringa3 =stringa2.join(' ');
for(let i=0; i<stringa.length; i ){
if(i == stringa.length-1){
if(stringa3[i]== "ò" || stringa[i]== "è" || stringa[i]== "à" || stringa[i]== "ì" || stringa[i]== "é" || stringa[i]== "ù" || stringa[i]== "ú" || stringa[i]== "í" || stringa[i]== "á" || stringa[i]== "ó"){
stringa3 = stringa3 apice;
}
}
console.log(stringa3);
}
CodePudding user response:
You might use a pattern checking for those chars at the end of a "word" with a character class for the chars and a lookahead to assert a whitspace boundary to the right.
[òèàìéùúíáó](?!\S)
In the replacement use the full match using $&
followed by '
See a regex demo for the matches.
let stringa = "nicolò à test Niccòlò Niccòlò";
const pattern = /[òèàìéùúíáó](?!\S)/g;
console.log(stringa.replace(pattern, "$&'"));