skcrpk
skcrpk

Reputation: 556

detect number in a string - javascript regex

how can i write a regex to find a phone number

this is a number 09876 09875

it should detect 09876 09875 as a whole number

this is a number +17865 8658 u98765

this should detect two numbers +17865 8658 and 98765

Upvotes: 0

Views: 189

Answers (2)

Mansoor Jafar
Mansoor Jafar

Reputation: 1488

Use following Regex

/[+0-9]+(?:\.[0-9]*)?/g

for working example click

Upvotes: 1

hsz
hsz

Reputation: 152206

Remove spaces and match plus sign with following numbers:

var input   = 'this is a number +17865 8658 u98765',
    outputs = input.replace(/ /g, '').match(/\+?\d+/g);

Output:

["+178658658", "98765"]

Without replacing spaces:

var input   = 'this is a number +17865 8658 u98765',
    outputs = input.match(/\+?\d( *\d+)+/g);

Output:

["+17865 8658", "98765"]

Upvotes: 1

Related Questions