6

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.

4
  • 2
    have you tried anything? where are you stuck? Commented Dec 14, 2016 at 14:16
  • 2
    What have you tried so far? Did you google "javascript regular expressions"? Commented Dec 14, 2016 at 14:17
  • Yes...I did google about it. Found about .match(), but can't seem to get it done. Commented Dec 14, 2016 at 14:18
  • [^A-Z] is the regular expression to match x and y Commented Dec 14, 2016 at 14:40

4 Answers 4

15

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.

Sign up to request clarification or add additional context in comments.

4 Comments

But it gives same result for 10M and 10S. How would I distinguish?
True, it does. As per the question, you said you want to extract the x and/or y's which are related to M and S respectively. If you supply same x and y values in the string, why do you want to distinguish between them (as both are same)?
And if you really want to distinguish between tose two, use the first element in match array, which gives the whole matched string.
Thank you so much for the help!!
2

you can use .split() to split the strings:

var str = "10M225S"
var m = str.split("M")[0];
var s = str.split("M")[1].split("S")[0];

console.log("m >>> " + m);
console.log("s >>> " + s);

no need for regular expressions in this case.

3 Comments

Thanks for the alternative. But I need a regex approach.
@dsaket Why? Because it's homework? If so then it's likely your tutor has given you the documentation you need.
No..it's a situation I encountered when I was working on an app.
0

K-Gun's answer makes sense if the pattern never varies, however if 'M' and 'S' are simply placeholder examples for arbitrary text, then use the RegExp /\d+/g

<!DOCTYPE html>
<html>
<head>
  <script>
    function fn() {
      var re = /\d+/g, // match digits, globally
        str = '10M225S',
        result = [],
        temp;
      // process successive matches
      while ((temp = re.exec(str)) !== null)
        result.push(temp[0]);

      document.getElementById("result").innerText = result.toString();
    }
  </script>
</head>
<body onload="fn()">
  <h1 id="result"></h1>
</body>
</html>

Here's a plunkr demonstrating the code.

The RegExp re will match runs of digits. This will work regardless of any non-digit characters, e.g. it would match "10ZXZZ225FOZ".

2 Comments

it gives the same result for 10M and 10S. i.e. 10. How would I distinguish?
@dsaket have a look at the plunkr I linked because that is incorrect.
0
var str = "10M255S"
var result = str.match(/([0-9])/g)

var value = []
result.map(res => {
  value += res
})
console.log(value)

2 Comments

Consider explaining your code, so OP and other future readers know what the purpose is and how it solved the problem.
Using .map() and not using the returned value? Did you want to use forEach()? And I'm not sure if it answers the question, as your current code returns a string like 10255, not what OP wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.