Voora Tarun
Voora Tarun

Reputation: 1216

Extract numbers from strings using regexpressions

Hi i am new to javascript ,I want to extract numbers from String .

Ex: i want to buy a phone between 10000 to 20000

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

this gives 10000 and 20000

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

I have to extract 10k and 20k exact match . Please give regexp for that.

Upvotes: 0

Views: 41

Answers (1)

Ehsan
Ehsan

Reputation: 12951

This help you :

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

example :

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

Upvotes: 2

Related Questions