Find the longest word : why this doesn't work?[solved]

Find the longest word : why this doesn't work?[solved]
0

#1

my original solution :

function findLongestWordLength(str) {
  let strRegex = /\w+\s*/g;
  let strings = str.match(strRegex);
  return Math.max(...strings.map(s=>s.length));  
}

The above doesn’t work, so i changed
let strings = str.match(strRegex);
into this
let strings = str.split(' ');
and i passed the test.

However, any explanation on why the original solution does not work?


#2

I haven’t run your code, but it looks like the Regex matches the whitespace following a word, so it would be included in the length. You could try putting the whitespace into a non-capturing group.


#3

Correct, thank you…