MJ
asked on
JavaScript Regex for 0-9/A-Z pattern match with Hyphen
I'm looking for a Javascript regx that will detect a-z (case insensitive) or 09 as a group and then a hyphen followed by any group of a-z or 09 etc. Always will start/end with (a-z or 09) For example:
a12dd3-23-fff-11n3N33n3-98 8
9fkr-rtt488-344-45
r5999999Rrp-4-yei388dfi3-3 444-fjj577 -23
xxxxxxxx-xxxx-xxxx-xxxx-xx xxxxxxxxxx
1234-567-8-922-222222-2-11 11
a12dd3-23-fff-11n3N33n3-98
9fkr-rtt488-344-45
r5999999Rrp-4-yei388dfi3-3
xxxxxxxx-xxxx-xxxx-xxxx-xx
1234-567-8-922-222222-2-11
ASKER
Hi zc2,
That doesn't seem to work, as it allows anything I've tested, good or bad?
That doesn't seem to work, as it allows anything I've tested, good or bad?
Could you post an input string which should not match?
ASKER
Hi Zc2,
Try these two:
*********8
s@#$%^^
Try these two:
*********8
s@#$%^^
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
HI,
I tested again and it still works for strings ending with a hyphen but it shouldn't. Examples that should fail:
xxxxxxxx-xxxx-xxxx-xxxx-xx xxxxxxxxxx -xxxx-
123-sx2M-34-
I tested again and it still works for strings ending with a hyphen but it shouldn't. Examples that should fail:
xxxxxxxx-xxxx-xxxx-xxxx-xx
123-sx2M-34-
[a-zA-Z0-9\-]+
https://regex101.com/r/hRXTDF/1
ASKER
Hi Shaun,
I believe it still allows strings ending with a hyphen which I do not want.
Thanks!
I believe it still allows strings ending with a hyphen which I do not want.
Thanks!
ASKER
This works: ^[a-zA-Z0-9]+(-[a-zA-Z0-9] +)*$
ASKER
Thanks!
Open in new window
https://regexr.com/3vf1b