javascript - Single RegEx to support both Email and Alphanumeric string -


i using following 2 patterns

1 - test alphanumeric without space , special characters '-','#','&'

var test =  /^([a-za-z0-9\.\-#&\s]{6,})*$/  - test.test('kumar'); // false (since 6 characters not there)  b - test.test('bibek1211');//true  c - test.test('bibek1211#-&');//true  d - test.test('bibek12 kumar');//false (since space there) 

2 - test email

 var test =  /^[a-za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-za-z0-9](?:[a-za-z0-9-]*[a-za-z0-9])?\.)+[a-za-z](?:[a-za-z]*[a-za-z])?$/  

now got requirement merge above 2 regex , put in 1 regex.this facing issues since have few knowledge in regex.

i want add above 2 regex , put in 1 single regex (both in c# , javascript)so 1 single regex can able validate following pattern

a - test.test('kumar'); // false (since 6 characters not there) b - test.test('bibek1211');//true c - test.test('bibek1211#-&');//true d - test.test('bibek12 kumar');//false (since space there) e - test.test('kumar@gmail.com');//true f - test.test('kumar@gmailcom');//false (not email) 

please me above . in advance !!

you can club 2 regexes using | or or.

^(?:(?:[a-za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-za-z0-9](?:[a-za-z0-9-]*[a-za-z0-9])?\.)+[a-za-z](?:[a-za-z]*[a-za-z])?)|([a-za-z0-9\.\-#&]{6,}))$ 

see demo.

https://regex101.com/r/ij3ds6/1


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -