regex - javascript regexp both character is optional but merely one is invalid -
i have string checking regular expression in javascript. string can contain +8
or 8
or can empty string. cannot contain merely +
sign.
these valid: +8
or 8
or ( 3rd 1 indicates empty string)
this invalid: +
the code have tried:
var x=/^\+?(8)?$/.test("+8")? 'ok':'not'; document.write(x);
everything ok code. problem that, code returns true if string contains only + sign
, should false. how implement have wanted?
try -
^(\+?8)?$
testing -
> /^(\+?8)?$/.test("+")? 'ok':'not'; "not" > /^(\+?8)?$/.test("+8")? 'ok':'not'; "ok" > /^(\+?8)?$/.test("")? 'ok':'not'; "ok"
Comments
Post a Comment