【问题标题】:How to find a number in a string using JavaScript?如何使用 JavaScript 在字符串中查找数字?
【发布时间】:2010-12-10 01:00:35
【问题描述】:

假设我有一个字符串 - “您最多可以输入 500 个选项”。 我需要从字符串中提取500

主要问题是字符串可能会有所不同,例如“您最多可以输入 12500 个选项”。 那么如何获取整数部分呢?

【问题讨论】:

  • 您应该点击cletus给出的答案的复选标记。这绰绰有余。

标签: javascript regex


【解决方案1】:

使用regular expression

var r = /\d+/;
var s = "you can enter maximum 500 choices";
alert (s.match(r));

表达式\d+ 表示“一位或多位数字”。默认情况下,正则表达式是greedy,这意味着它们会尽可能多地抓取。另外,这个:

var r = /\d+/;

相当于:

var r = new RegExp("\d+");

请参阅details for the RegExp object

上面将抓取 first 组数字。您也可以循环查找所有匹配项:

var r = /\d+/g;
var s = "you can enter 333 maximum 500 choices";
var m;
while ((m = r.exec(s)) != null) {
  alert(m[0]);
}

g(全局)标志是此循环正常工作的关键。

【讨论】:

    【解决方案2】:

    var regex = /\d+/g;
    var string = "you can enter maximum 500 choices";
    var matches = string.match(regex);  // creates array from matches
    
    document.write(matches);


    参考资料:

      http://www.regular-expressions.info/javascript.html

       https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

    【讨论】:

      【解决方案3】:
      var str = "you can enter maximum 500 choices";
      str = str.replace(/[^0-9]/g, "");
      console.log(str); // "500"
      

      【讨论】:

        【解决方案4】:

        我喜欢@jesterjunk 的回答, 但是,数字并不总是数字。考虑这些有效数字: "123.5, 123,567.789, 12233234+E12"

        所以我刚刚更新了正则表达式:

        var regex = /[\d|,|.|e|E|\+]+/g;
        
        var string = "you can enter maximum 5,123.6 choices";
        var matches = string.match(regex);  // creates array from matches
        
        document.write(matches); //5,123.6
        

        【讨论】:

        • 试过你的正则表达式了吗?它匹配任何e 字符(或该列表中的任何其他字符,“E”、“”或“.”)。对于您的输入matches[ 'e', 'e', '5,123.6', 'e' ]
        【解决方案5】:

        var regex = /\d+/g;
        var string = "you can enter 30%-20% maximum 500 choices";
        var matches = string.match(regex);  // creates array from matches
        
        document.write(matches);

        【讨论】:

          【解决方案6】:

          你也可以试试这个:

          var string = "border-radius:10px 20px 30px 40px";
          var numbers = string.match(/\d+/g).map(Number);
          console.log(numbers)

          【讨论】:

            【解决方案7】:

            我想我会添加我的看法,因为我只对第一个整数感兴趣,我把它归结为:

            let errorStringWithNumbers = "error: 404 (not found)";        
            let errorNumber = parseInt(errorStringWithNumbers.toString().match(/\d+/g)[0]);
            

            .toString() 仅当您从获取错误中获取“字符串”时才会添加。如果没有,那么您可以将其从行中删除。

            【讨论】:

              【解决方案8】:
              // stringValue can be anything in which present any number
              `const stringValue = 'last_15_days';
              // /\d+/g is regex which is used for matching number in string
              // match helps to find result according to regex from string and return match value
               const result = stringValue.match(/\d+/g);
               console.log(result);`
              

              输出将是 15

              如果您想了解更多关于正则表达式的信息,请点击以下链接:

              https://www.w3schools.com/jsref/jsref_obj_regexp.asp

              https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

              https://www.tutorialspoint.com/javascript/javascript_regexp_object.htm

              【讨论】:

                【解决方案9】:

                现在使用 'replace' 方法和 'regexp' 很容易做到这一点。例如:

                findNumber = str => {
                  return +(str.replace(/\D+/g, ''));
                }
                
                console.log(findNumber("you can enter maximum 500 choices"));
                console.log(findNumber("you can enter maximum 12500 choices"));

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-01-26
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-03-22
                  • 1970-01-01
                  • 2022-12-16
                  相关资源
                  最近更新 更多