最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Using Regular Expressions to Extract Values in Javascript - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Dec 14, 2016 at 14:37 dsaket asked Dec 14, 2016 at 14:15 dsaketdsaket 1,8962 gold badges20 silver badges30 bronze badges 4
  • 2 have you tried anything? where are you stuck? – Cruiser Commented Dec 14, 2016 at 14:16
  • 2 What have you tried so far? Did you google "javascript regular expressions"? – slim Commented Dec 14, 2016 at 14:17
  • Yes...I did google about it. Found about .match(), but can't seem to get it done. – dsaket Commented Dec 14, 2016 at 14:18
  • [^A-Z] is the regular expression to match x and y – DanieleO Commented Dec 14, 2016 at 14:40
Add a comment  | 

4 Answers 4

Reset to default 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.

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.

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

var str = "10M255S"
var result = str.match(/([0-9])/g)

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

评论列表(0)

  1. 暂无评论