最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find first longest word in a string, excluding symbols - Stack Overflow

programmeradmin6浏览0评论
function longestWord(string) {
    var str = string.split(" ");
    var longest = 0;
    var word = null;
    for (var i = 0; i < str.length; i++) {
        var checkedLetters = "";
        for (var j = 0; j < str[j].length; j++) {
            if (j == /[^a-zA-Z]/) {
                checkedLetters += j;
            }
        if (longest < checkedLetters.length) {
            longest = checkedLetters.length;
            word = checkedLetters;
           }
        }
    }
    return word;
}

Is there something wrong with my use of regex? When I call longestWord("Hello, I am here") I want it to return "Hello" (without the ma), but it returns null.

function longestWord(string) {
    var str = string.split(" ");
    var longest = 0;
    var word = null;
    for (var i = 0; i < str.length; i++) {
        var checkedLetters = "";
        for (var j = 0; j < str[j].length; j++) {
            if (j == /[^a-zA-Z]/) {
                checkedLetters += j;
            }
        if (longest < checkedLetters.length) {
            longest = checkedLetters.length;
            word = checkedLetters;
           }
        }
    }
    return word;
}

Is there something wrong with my use of regex? When I call longestWord("Hello, I am here") I want it to return "Hello" (without the ma), but it returns null.

Share Improve this question edited Jun 30, 2013 at 5:01 Charlie 11.8k19 gold badges87 silver badges140 bronze badges asked Jun 30, 2013 at 4:38 bardbard 3,07211 gold badges38 silver badges52 bronze badges 2
  • 1 What if there are 2+ longest words? – jeremy Commented Jun 30, 2013 at 4:39
  • Then I would want it to return the first longest word it es across. – bard Commented Jun 30, 2013 at 4:42
Add a ment  | 

4 Answers 4

Reset to default 5

Just wrote this little snippet, might help you out:

function longestWord(string){
    return string.match(/[a-zA-Z]+/g)
               .reduce(function(a,b){
                    return a.length>=b.length?a:b;
               })
}

/[a-zA-Z]+/g matches all words in the string, and returns an array of them. Your test string above ("Hello, I am here") will bee ["Hello","I","am","here"] when this RegEx is run on it.

Once I have this array, it is simply a matter of looping through it to find the longest word. I acplished this by using .reduce.

There are some mistake in your code:

for (var j = 0; j < str[j].length; j++) {

should be

 for (var j = 0; j < str[i].length; j++) {

And

if (j == /[^a-zA-Z]/) {

Should be:

if (/[a-zA-Z]/.test(str[i][j])) {

Your final code should be:

function longestWord(string) {
    var str = string.split(" ");
    var longest = 0;
    var word = null;
    for (var i = 0; i < str.length; i++) {
        var checkedLetters = "";
        for (var j = 0; j < str[i].length; j++) {
            if (/[a-zA-Z]/.test(str[i][j])) {
                checkedLetters += str[i][j];
            }
        }
        if (longest < checkedLetters.length) {
            longest = checkedLetters.length;
            word = checkedLetters;
        }
    }
    return word;
}

Check demo

The big (non-typo) problem with how you’re using regular expressions is that the method is .test; == will test whether the string is equal to the string representation of the regular expression.

Just use .match and a little bit of sort magic!

function longestWord(string){
    var words = string.match(/\w+/g);
    words.sort(function(a, b) { return b.length - a.length; });
    // Firefox 22.0 promotion:
    // words.sort((a, b) => a.length - b.length);

    return words[0];
}

You don't need to use Regex, you can just use .length to search for the longest string.

i.e.

function longestWord(string) {

    var str = string.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ");
    longest = str[0].length;
    longestWord = str[0];

    for (var i = 1; i < str.length; i++) {
        if (longest < str[i].length) {
            longest = str[i].length;
            longestWord= str[i];
           }
        }

return longestWord;
}

EDIT: You do have to use some Regex...

发布评论

评论列表(0)

  1. 暂无评论