使用regexpressions从字符串中提取数字

时间:2016-06-25 06:32:13

标签: javascript regex node.js

您好我是javascript的新手,我想从String中提取数字。

Ex:我想购买10000到20000之间的手机

message = message.replace(/ /g, '')
var start_num = message.match(/\d+/g)[0];
var end_num = message.match(/\d+/g)[1];

这给出10000和20000

I want to buy a phone between 10k and 20k

我必须提取10k和20k完全匹配。请为此提供正则表达式。

1 个答案:

答案 0 :(得分:2)

这有助于您:

var patt = /\d+k?/gi;

示例:

<html>
<head></head>
    <body>
         <script>
             var i=0;
             var patt = /\d+k?/gi; //if want only 1000k and no 1000 so remove '?'
             var str = "I want to buy a phone between 10k and 20k";
             alert(str.match(patt));
             
        </script>
    </body>
</html>