Check if String contains only Spaces in JavaScript | bobbyhadz
Cet algorithme fonctionne, néanmoins, si il y a plus d’un espace entre les mots, cela ne fonctionne plus.
function generateHashtag (str) {
if(str.length === 0 || str.trim().length === 0 || str.length > 139) return false;
const _arr = str.split(" ").map(w => w[0].toUpperCase() + w.substring(1));
console.log(_arr);
_arr.splice(0,0,"#");
return _arr.join("");
}
Le replace permet d’enlever tout les espaces sauf un quand il y a une récurrence supérieure à 1
function generateHashtag (str) {
if(str.length === 0 || str.trim().length === 0) return false;
const _arr = str.replace(/ +/g, ' ').split(" ").map(w => w[0].toUpperCase() + w.substring(1));
if(_arr.join("").length > 139) return false;
_arr.splice(0,0,"#");
return _arr.join("");
}