Reputation: 781
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
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.
Upvotes: 0
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
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
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
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
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