Sunday, December 9, 2018

Javascript Regular Expressions


Basic Validations using reg exp: 

 Check Numbers : /\d+/g
 AlphaNumaric: /^[A-Z0-9]*$/
 AllZeros: /^[0]*$/
 12 Numbers : /\b\d{12}\b/
Any Sequence in any format :
Ex1 : /\b[X]{1}[A-Z]{1}\d{6}[A-Z0-9]{1}\b/
output is : X123456A || X1234569

Ex2 : /\b[U]{1}[F]{1}\d{5}[A-Z]{1}\b/ 
output is : UF12345A



var regExp="/\b\d{6}\b/";
var sampleStr="123456"
regExp.test(sampleStr) returns true.


To test the sample data use below online testers

https://regex101.com/
https://www.regextester.com/

How to test reg Exp :
reg Exp : \b\d{4}\b

if you enter 1234 it will show 1 Match found
other than this it will show No Match found


No comments:

Post a Comment