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-开发者_Go百科Za-z0-9]+$/;
But this isn't the right way. Im new to this, help anyone?
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].*$/;
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.
Try /^[0-9]/
as the regular expression.
If it only needs to start with a number, I'd only check that...
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].+$/;
I suppose this should work
/^\d+.+$/
I would get rid of the $. Also, a '.' would suffice for "any character". This one works for me:
var validaddress = ^[0-9].+;
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
You could also use /^\d/
as the briefest approach.
精彩评论