Find the longest word in a string using javascript

后端 未结 10 1235
长情又很酷
长情又很酷 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:49

    function findLongestWord(str) {
      //This is what I  used to find how many characters were in the largest word
      return str
              .replace(/[^\w ]/g,'')                            
              .split(' ')                                       
              .sort(function(a, b) {return a.length-b.length;}) 
              .pop().length;                                           
    
    }
    findLongestWord('The quick brown fox jumped over the lazy dog');
    

提交回复
热议问题