Continuing the discussion from freeCodeCamp Challenge Guide: Find the Longest Word in a String:
Hello, friends and everyone present in the community to whom I owe so much for letting all of us share and learn for free. Anyways I’ll not waste your time and come to the point that there is another way of doing this particular problem { Find the Longest Word in a String} using regex. Yes, you heard me right REGEX. Those complex statements of .match() and .test(). I’m posting this solution in the community so anyone interested can ask questing and since we have been through the REGEX module, it will be easy to understand for you. Thank you and if you like the solution then feel free drop a heart. Happy coding to all.
function findLongestWordLength(str) {
let x = /.[a-z].?\s|.[a-z].?/gi;
let y = str.match(x);
//return y;
let z= ;
for (let i = 0; i< y.length-1 ;i++){
if (y[i].length > z.length){
z = y[i];
}
}
if (y[y.length - 1].length > z.length){
z = y[y.length - 1];
return z.length;
}
else{
return z.length - 1;
}
}
console.log(findLongestWordLength(“What if we try a super-long word such as otorhinolaryngology”));