2

I have:

var str = 'ddd';
var r = /d*/g;
console.log(str.match(r))

the output in my console is an array of two; first is a string containing 'ddd' and second its a empty string. I know what * means (0 or more) but why it outputs that empty string? I would expect to ouptut just one string 'ddd' . Once it matches for d key, why it just continue with that empty string?

If I call without the g flag, it outputs what I really expected to do with it.

I also know that g means, global search, it iterates through all elements of str, but how that, when my search was completed by the first match?

4
  • change the * to a + if you do not want a zero match? Commented Jul 29, 2015 at 13:33
  • 3
    The 'g' flag indicates that after the first match is found (d* = "ddd") it should continue to find matches, which it does: the empty string that is left after "ddd" matches the regexp d* by counting 0 d's. Commented Jul 29, 2015 at 13:34
  • well that is what I don`t understand, why my string is left empty after the first match? Commented Jul 29, 2015 at 13:53
  • It's still confusing: if my string was 'bddd' it woud add a single empty string at the beginning of the array at index 0. So it matches 'd' and says: hey that's 0 so I'll add an empty string element. The second element at index 1 'ddd' also makes sense. But the third empty element doesn't make sense. Why would it add the last empty string but not the very first, before 'd'? Or for that matter, the empty strings between all the characters? Commented Feb 18, 2022 at 9:24

2 Answers 2

3

It's logical that you got two matches here:

  • First one is 'ddd':

    which is logical with d*.

  • Second one is ' ' :

    which is logical too with d*, because the * means zero or many times and that's zero d here, and it's not a white space, just an empty string so basically any string will be matching it.

You should have been using d+ instead.

EDIT:

You can see it in this DEMO that with ddd the matches are:

  1. [0-3] ddd

  2. [3-3] ``

It's taking the last of the string as a match here with the index : [3-3]

Sign up to request clarification or add additional context in comments.

13 Comments

well, I know that + would solve that problem, but my question is why it just matches with an empty space? because I don't have that in it. It's weird to evaluate the first condition, wich is fine, and then evaluate with an empty space, wich I haven`t introduced in my string...Now I understood that after the first condition, my string goes empty, wich, in my opinion is fully retarded, and evaluates as true
Strings are full of empty strings. 'abcde'.match(/()/g) -- there's six for you.
It's not an empty space but just any string even an empty one, take a look at my EDIT.
so, my last question would be, why the hell, my string goes empty after the first match?
Do you see anything else there?
|
3

The pattern d* means "zero or more occurrences of 'd'". Zero occurrences of 'd' is the empty string. The greedy * first matches all the 'd' characters in the string, and then matches the nothing between the last 'd' and the end of the string.

The g flag on the pattern tells the regular expression matching mechanism to iteratively re-try the match. The first iteration matches all of the 'd' characters. At that point, the remaining string is basically the empty string; a string with zero length. The match succeeds in the same way that "".match(/d*/g) would succeed.

8 Comments

well how that if it just match the first string by 'ddd' result?
@GigiIonel I don't understand the question.
really? it just matches between the last 'd' and the end of the string? come on bro` ...this just sucks! who the hell made this shit? I mean, it's not like my string contains a blank space to be matched...it`s weird
@GigiIonel your pattern says, "match zero or more instances of the character 'd', as many times as you find it in the string". That's exactly what's happening.
@GigiIonel note that "".match(/d*/g) will succeed too. That's just what * means in a regular expression.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.