I have a string in the form: "xMyS"
I want to extract x and y in using the Javascript Regex expressions.
For example, if the string is
10M225S
I need 10 and 225 from this.
Also, the string might not have either of x or y part. For example it can be just 225S or just 10M.
EDIT: I tried things like .match(), but don't know how to extract values in the mentioned format.
EDIT: The regex I tried so far is /(\w+)M(\w+)S/, but it seems to only work for 10M225S and not for 10M and 225S.
You can do something like this:
var str = "10M255S"
var match = str.match(/^([0-9]*)M*([0-9]*)S*$/)
Then match[1] is 10 and match[2] is 255
If var str = "10M", then match[1] is 10 and if var str = "255S", then match[1] is 255
In any of the three cases, matches start from second element of the array match.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With