Regular expression questions

hello,

why do i see 12 values in this array when i can reed only 11?

 var str = "42 33 fat 3";
          console.log(str.match(/\d?/g));


and another thing:

why this code returns false:

 console.log(/^a?$/.test("b"));

it looks for zero occurrence start to end.

  1. Good question, it seems to add one for the end position, since nothing is there.
  2. Yeah, but nothing else. You’re matching “only an a” or “nothing”. “b” doesn’t match that.
1 Like

I question the utility of matching this type of expression. Anyway, I suspect that this has something to do with what happens when you match without an argument (str.match()) or with an empty string (str.match("")). In both these case the output is [""].

1 Like

hey lyn,

1- so why it adds another value?

2- is’t b a nothing? like no match?

  1. Don’t know, could be an implementation thing. But you’re using a contrived example, eg. the regex would make more sense without the ? in this context or you could use test. Here are the docs if you want to read the specification: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
  2. No, “b” is not equal to “”. The expression is the same as /^(a|)$/, matching only what would be ^$ and ^a$. So why would ^b$ fit?

Can you give an example here? I see 12 values.

The reason for 12 matches with your regular expression is that it matches every character in the string as well as the empty string.
I have found sites like regex101 very useful in understanding how regular expressions work.

1 Like

Thanks for the link - that’s a really useful site. I’m still not sure I get it, though. If I look at the output from https://regex101.com with test string 1 2 3 and regex /\d?/g you can see:

match 1 : 0 - 1 "1"
match 2 : 1 - 1 ""
match 3 : 2 - 3 "2"
.
.
match 6 : 5 - 5 ""

0 - 1 seems to be the start and end ‘lines’ of each match - these are the places you can put the cursor and it always seems to include the end one ( 5 - 5 in this case ) . It’s matching the digits but when they aren’t there, it’s matching anyway and returning the cursor position (an empty string?). It does this one last time at the end, even though there’s no character left to come.

I only touch regex when I have to and I always wash my hands (brain) afterwards. For anyone else wondering, the ‘right’ way to do this is with console.log(str.match(/\d+/g)).

if you count all characters it will sum up to 11

hey,

There is no empty string or space at the end. why does it include the beginning as well?