Return only part of a regex

I have the following regex: /[ML](\d+\.*\d*),/g

I also have a string that looks something like this:
‘M0,1.23L1,2343L123,23.23’

I basically want to have a regex, that returns [0,1,123] when matched with my string.
Basically I want to make sure, that I only get the first of the two numbers that follow a letter and are seperated by a comma.

Is there a nice way to do this?

1 Like

What you are looking for is called “capture groups”. Here’s one guide to capture groups.

4 Likes

Thanks for the answer! Unfortunately I seem to be too incompetent to figure out how to acutally do it :smiley:

Or am I correct in assuming, that capture groups only have an effect when the .exec() function is called?

Currently this is what I would do:

let str = 'M0,231L12,3245.234L1.35,2553'

let regex = /[ML](\d\.*\d*),/g

console.log(str.match(regex))

but this returns an array of the entire matches. So am I missing something? How would I return an array of only the capture group matches?

1 Like

You’re on the right track. You’re going to end up with a collection of matches. Depending on what you want you can grab them based on their order, iterate over them to find your favorite, or you can use named capture groups (you’ll find good explainers for that with Google) to get a specific one.

3 Likes

Hi!
Check the following code:

reg = /(\d+\.*\d*),/g

"M0,1.23L1,2343L123,23.23".match(reg).map(x=>parseInt(x))

Is that what you are looking for?

2 Likes

That’s areally good pointer/ guide you’re offering here. I use this too for regex issues.
Thanks

2 Likes

For my specific case this actually works, so that’s quite nice. However it seems, that in this case: reg = /\d+\.*\d*,/g also works. I am wondering, if there is a way to achieve this within the regex. So far I don’t seem to be able to do it.

1 Like

Just to double check: Named capture groups only return the first match correct?

Thanks for your help!

1 Like
reg = /(\d+\d*)(?=,)/g

"M0,1.23L1,2343L123,23.23".match(reg)

The result is:
[ "0", "1", "123" ]

If you would like to have only integers in the array you have to use map and parseInt as mentioned before!

2 Likes

Ok, it almost clicked. I already thought it did, but after some tests, I realised I still didn’t fully get it (and feel like I definetely should by now).

reg = /(?=[ML])(\d+\d*)(?=,)/g

"M0,1.23L1,2343L123,23.23".match(reg)

Why is the result null here? I expected it to be the same as in your example, since all three matches follow either an M or an L (meaning [ML] in regex) and I have excluded them from being part of the result by putting them in parantheses and adding a ‘?=’ in front of them.

Thanks so much for your help, this is really bugging me.

2 Likes

Hi,

I think you have to use ^ before the M to exclude ML to be part of the result ==>
Use [^ML] instead of [ML]. After doing it the result of:

reg = /(?=[^ML])(\d+\d*)(?=,)/g
"M0,1.23L1,2343L123,23.23".match(reg)

will be:

[ "0", "1", "123" ]

:+1:

2 Likes

Wouldn’t [^ML] just match any character not present in the list? What I would like to do instead, is to match the pattern of (M or L), (a number), (a comma). but only have the number shown in the array returned by .match.

So pretty much, just like we excluded the comma from showing up (hopefully I understood correctly, that this is actually what we did) I would like to exclude [ML], but still make sure, that the number I want to get is preceeded by that.

1 Like

have you tried with a lookbehind?

the issue with a lookahead is that it doesn’t advance so if you have (?=[ML])\d) you are trying to have a letter and a number in the same place

3 Likes

Hi,

I hope that now I understand your question !
I think the solution should be:

reg = /(?<=[ML])\d+/g

"M0,1.23L1,2343L123,23.23".match(reg)

I have used the following pattern:

(?<=y)x---> Positive lookbehind --->x if after y

I hope it helps a little!

2 Likes

Finally. Yes this is exactly it. To be 100% precise, using all the previous anwers, the following is what I wanted:
/(?<=[ML])(\d+\.*\d*)(?=,)/g

Perfect, thanks so much for the help!!

3 Likes

You are welcome! Enjoy coding!

2 Likes

Congratulations on working it out. Happy coding!

3 Likes