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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -