Don Rhummy
Don Rhummy

Reputation: 25870

How find a phone number in a string with other content?

I have a regex that matches a phone number but only if it's the only thing in a string. I need to be able to find a phone number in something like: "This is your phone number: 111.222.1111. OK?".

My current Regex:

/(^\(\d{3}\)( |-|\.)?\d{3}( |-|\.)?\d{4}$|^\d{3}( |-|\.)?\d{3}( |-|\.)?\d{4}$|^[0-9]{10,10}$)/g

How can I modify this to find phone numbers within a string?

Upvotes: 0

Views: 103

Answers (1)

friedi
friedi

Reputation: 4360

Remove all of your ^ and $ symbols in the regex:

/(\(\d{3}\)( |-|\.)?\d{3}( |-|\.)?\d{4}|\d{3}( |-|\.)?\d{3}( |-|\.)?\d{4}|[0-9]{10,10})/

Here is the example

Upvotes: 1

Related Questions