Find the longest word in a string using javascript

后端 未结 10 1239
长情又很酷
长情又很酷 2021-01-16 23:06

I\'m trying to find the longest word in a string, but it continually returns the length of the first word. Any ideas?

Here\'s my code:

function findL         


        
10条回答
  •  南方客
    南方客 (楼主)
    2021-01-16 23:45

    I would do something like this:

    function longestWord(str){
      return str.match(/\w+/g).reduce((p,c) => p.length > c.length ? p.length:c.length);
    }
    

提交回复
热议问题