Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

regex javscript

======
Match:
======

	[^pe]000 = except p and e everything match
	
	[pe]000	= only match p and e

=======
Ranges:
=======

	[a-z] , [b-h], [start, end]
	
	[a-zA-Z] -  both uppercase, lowercase
	
	[0-9] - 0 to 9 only
	
	but how 11 digits?? not single number??
	
	[0-9]+ - unlimited digits
	
	[0-9]{11} - only 11 digits
	
	[a-z]{11} - 11 letter word
	
	[a-z]{5,8} - 5 chars upto 8 chars
	
	[a-z]{5,} - minimum 5 and unlimited
	

==============
MetaCharacters:
==============
	
	Must have backslash on front otherwise it will behave as char...

	d - match any digit character (same as [0-9]) 
	w - match any word character (a-z, A-Z, 0-9 and _ underscores)
	s - match a whitespace char (spaces, tabs etc)
		 - match a tab char only
	
	EX:  d{3}sw{5}  -> first 3 numbers then space then 5 word chars
	
==============
Special Characters:
==============	

		+  -> one or more quantifier
	 
          ->  escape character or backslash
	
       []  ->   charset
	
       [^] ->	negate
       
       ?   ->   zero or 1     ex: a[a-z]?   must have a after that optional
       
       .   ->  any char except new line   
       			
       			 ex:  car.    carx true not newline char
       			 ex:  .+  any length string
       			 
       *   ->  0 or more bit like +
       
	       		ex: abc* - now include * aswell for match
       
       examples:
       
       ^w{5}$ -> must be 5 words for match
       
       			^ -> start
       			$ -> end
	
       (pet|toy|crazy)?rabbit
	
//===============================================================================//
// const reg2 = new RegExp(/[a-z]/,'i')
// const reg = /[a-z]/gi; // g-global, i-insensitive

const patterns = {
  tel: /^d{11}$/,
  username: /^w{5,12}$/i,
  pswd: /^[w@-]{8,20}$/,
  slug: /^[a-zd-]{8,20}$/,
  // 4 parts of email
  email: /^([a-zd.-]+)@([a-zd-]+).([a-z]{2,8})(.[a-z]{2,8})?$/,
};

const validate = (value, regex) => {
  const reg = regex.test(value);
  if (reg) {
    // add css class 'valid'
    return "Valid";
  } else {
    // add css class 'invalid'
    return "Invalid";
  }
};

validate("admin.fdsfsdfdsf@gmail.com", patterns["email"]);
	
//===============================================================================//
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #regex #javscript
ADD COMMENT
Topic
Name
1+2 =