Reputation:
I am trying to match a string with a regex condition. It doesn't seem to be working though. It will always be capital D
var string = 'D123';
var matchVar = string.match(/^D+[0-9]^/);
if(matchVar){
alert('yes');
}
DEMO http://jsfiddle.net/chwprLg1/
Upvotes: 1
Views: 7858
Reputation: 67968
^D+[0-9]+$
Guess you wanted this.^
asserts start of string.See demo.
https://www.regex101.com/r/rC2mH4/13
or
^D+\d+$
var string = 'D123';
var matchVar = string.match(/^D+\d+$/);
if(matchVar){
alert('yes');
}
Upvotes: 0
Reputation: 174706
You need to replace the last ^
with +
. +
repeat the previous token one or more times. So [0-9]+
matches one or more digits. You cud use \d
instead of [0-9]
var matchVar = string.match(/^D+[0-9]+/);
Without the end of the line anchor, the above regex would also match D98
in D98foobar
.
OR
One or more D
's followed by any number of digits.
var matchVar = string.match(/^D+[0-9]+$/);
OR
A single letter followed by any number of digits.
var matchVar = string.match(/^D[0-9]+$/);
Upvotes: 2