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.
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.
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.
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).
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.
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.