Parenthesis in regex

hello,

I am trying to capture the URL using regex and i get a result I don’t understand.

This is the example link:

var web = “www.example.com?cid=23233&cid=434343”;

If i match: web.match(/^(.*?)/)

i get only “” in the console log

but, If i match:

web.match(^(.*?)\?.*) 

in the console log i get the entire link in the first element, and the parenthesis (this time not only with “”) with the link until the ?.

Why the parenthesis manage to capture the link this time, but in the first time they capture only “”
?

That’s because you’re using the lazy qualifier ? in the capturing group. It matches the smallest possible number of characters and that is 0 because you used * to match 0 or more characters. That’s why you get the empty string as a match.

The second regex will also try to capture the smallest number of characters for the capturing group but since it’s also required to match character ? followed by any number of characters, the match only works for all characters up to ?. So the capturing group is “example.com” and the match is the entire string.

Use a regex tool to visualise and tinker with your regexes. It makes it easier to understand and debug. I used https://regexr.com to answer your question.

1 Like

thanks @sfiquet. if i understand right, the \? forced the capturing expression to work, and to capture www.example.com

the regular expression fix website looks promising!