thatguy
thatguy

Reputation: 39

Having trouble with regular expressions

I actually have 2 questions. There appears to be a knowledge gap in my understanding of regular expressions, so I was wondering if somebody could help me out.

1)

function LongestWord(sen) { 
    var word = /[^\W\d]([a-z]+)[$\W\d]/gi;

    var answer = word.exec(sen);  


    return answer;
}
console.log(LongestWord("9hi3"));

Why does this return [hi3, i] as opposed to [9hi3, hi] as intended. I am clearly stating that before a letter comes either the beginning, a number, or a non word character MUST be in my match. I also have the + symbol which being greedy should take the entire group hi.

2)

function LongestWord(sen) { 
    var word = /[\b\d]([a-z]+)[\b\d]/gi;

    var answer = word.exec(sen);  


    return answer;
}
console.log(LongestWord("hi"));

More importantly, why does this return null. #1 was my attempted fix to this. But you get the idea of what I'm trying to do here.

PLEASE TELL ME WHAT IS WRONG WITH MY THINKING IN BOTH PROBLEMS RATHER THAN GIVING ME A SOLUTION. IF I DON'T LEARN WHAT I DID WRONG I WILL GO ON TO REPEAT THE SAME MISTAKES. Thank you!

Upvotes: 2

Views: 48

Answers (1)

forgivenson
forgivenson

Reputation: 4435

Let's walk through your regular expressions, using your example string: 9hi3

1) [^\W\d]([a-z]+)[$\W\d]

First, we have [^\W\d]. Normally, ^ matches the start of the string, but when it is inside [], it actually negates that block. So, [^\W\d] actually means any one character that IS a word character, and not a digit. This obviously skips the 9, since that is a digit, and matches on the h.

The next part, ([a-z]+), matches what you are expecting, except the h was already matched, so it only matches the i.

Then, [$\W\d] is matching a $ symbol, a non-word character, or a digit. Notice that just like ^, the $ does NOT match the end of the string when inside the [].

2) [\b\d]([a-z]+)[\b\d]

For this one, you should start by looking at the documentation for exec to see why it can return null. Specifically:

If the match fails, the exec() method returns null.

So, you know that the match is failing. Why?

Again, your confusion is coming from not understanding how special characters change meaning when inside []. In this case, \b changes from matching a word-boundary, to matching a backspace character.

It is worth noting that your second regex will match the string you tested your first one with, 9hi3, because it begins and ends with digits. However, you tested it with hi.

I hope these explanations have helped you.

For future reference, you should take a look at the RegExp guide on MDN.

Also, a great tool for testing regular expressions is regexpal. I highly recommend using it to help you figure out exactly what your regular expressions are doing.

Upvotes: 2

Related Questions