Hriskesh Ashokan
Hriskesh Ashokan

Reputation: 781

Javascript regex to check if a value begins with a number followed by anything else

How would i go about doing a regex to see if it begins with a number and any character can follow after. My current expression is

var validaddress = /^[0-9][A-Za-z0-9]+$/;

But this isn't the right way. Im new to this, help anyone?

Upvotes: 2

Views: 16656

Answers (8)

JWL
JWL

Reputation: 14201

You could also use /^\d/ as the briefest approach.

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337580

If you have a string of these values and you want to find each individually try this:

(^|(?<=\W))(\d\w*)

You can then do a loop through each match.

Regexr example

Upvotes: 0

WEFX
WEFX

Reputation: 8562

I would get rid of the $. Also, a '.' would suffice for "any character". This one works for me:

var validaddress = ^[0-9].+;

Upvotes: 0

Aditya Manohar
Aditya Manohar

Reputation: 2274

I suppose this should work

/^\d+.+$/

Upvotes: 0

shinkou
shinkou

Reputation: 5154

If you need character(s) after the digit, try this:

var validaddress = /^[0-9].+$/;

If characters after the digit are optional, use this:

var validaddress = /^[0-9].*$/;

Upvotes: 9

Liv
Liv

Reputation: 6124

when you say "any character follow" -- do you mean any alphanumeric character or just anything (i.e. including space, comma, slash etc)? if it is the latter, how about this:

var validaddress = /^[0-9].+$/;

Upvotes: 0

Matt
Matt

Reputation: 75317

Try /^[0-9]/ as the regular expression.

If it only needs to start with a number, I'd only check that...

Upvotes: 0

enoyhs
enoyhs

Reputation: 2069

What you looking for is: var validaddress = /^\d.*$/;

\d - Matches any digit
.* - Matches any character except newline zero or more times.
Or replace .* with .+, if you are looking for at least 1 character.

Upvotes: 1

Related Questions