Is there any way to find out the longest word in Javascript? It should ignore punctuation marks too!
I understood the logic, but the code… sigh
Here’s what we do –
-
Count the number of alphanumeric characters that are together, not separated by a space or any sign.
-
Get their lengths.
- Find the biggest length in all.
- Return the word with the biggest length.
Hope I’m making myself clear…
Split the string, loop over the parts and keep track of the longest one.
Something like this:
If you need to split on non alphabetic characters as well as spaces you need to use regexes. You can change this line:
To this (thanks Kooilnc for the regex):
Working jsfiddle