Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript regex reference

// Javascript Regex Reference
//  /abc/	A sequence of characters
//  /[abc]/	Any character from a set of characters
//  /[^abc]/	Any character not in a set of characters
//  /[0-9]/	Any character in a range of characters
//  /x+/	One or more occurrences of the pattern x
//  /x+?/	One or more occurrences, nongreedy
//  /x*/	Zero or more occurrences
//  /x?/	Zero or one occurrence
//  /x{2,4}/	Two to four occurrences
//  /(abc)/	A group
//  /a|b|c/	Any one of several patterns
//  /d/	Any digit character
// /w/	An alphanumeric character (“word character”)
//  /s/	Any whitespace character
//  /./	Any character except newlines
//  //	A word boundary
//  /^/	Start of input
//  /$/	End of input
Comment

using regex in javascript

//Adding '/' around regex
var regex = /s/g;
//or using RegExp
var regex = new RegExp("s", "g");
Comment

javascript Create a RegEx

const reguarExp = new RegExp('abc');
Comment

what is regular expression in javascript

let re = /ol/, 
Characters , ., cX, d, D, f, 
, 
, s, S, 	, v, w, W, , xhh, uhhhh, uhhhhh, []	
Assertions 	^, $, x(?=y), x(?!y), (?<=y)x, (?<!y)x, , B
Groups 		(x), (?:x), (?<Name>x), x|y, [xyz], [^xyz], Number	
Quantifiers *, +, ?, x{n}, x{n,}, x{n,m}
Unicode p{UnicodeProperty}, P{UnicodeProperty}	 property escapes
let defaults = new RegExp('compiled'); 
defaults = { dotAll: false, flags: "", global: false, ignoreCase: false, falselastIndex: 0, multiline: false, source: "abc", sticky: false, unicode: false}
Comment

regex in javascript

const regularExp = /abc/;
Comment

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"]);
	
//===============================================================================//
Comment

PREVIOUS NEXT
Code Example
Javascript :: get the value of an input nativscript 
Javascript :: javascript get type of var 
Javascript :: constant expression contains invalid operations laravel 
Javascript :: check if value is number 
Javascript :: javascript converting an array into a map 
Javascript :: chrome storage local example 
Javascript :: save networkx graph to json 
Javascript :: react increment multipying button click 
Javascript :: Regex Chords 
Javascript :: configuration react-router-dom v6 
Javascript :: get blob from file javascript 
Javascript :: run react app 
Javascript :: add id to Array of Object 
Javascript :: javascript edit h tag value 
Javascript :: get url of website javascript 
Javascript :: js variable 
Javascript :: discord js send message to specific channel 
Javascript :: access mouse position javascript 
Javascript :: let var diferencia 
Javascript :: mongoose connection in express 
Javascript :: convert curl response to json format and echo the data 
Javascript :: removes null and false values from an array 
Javascript :: javascript get tag child elements 
Javascript :: javascript add to object 
Javascript :: how to enable emit on react in vs code 
Javascript :: js create object with keys 
Javascript :: javascript fill 2 dimensional array 
Javascript :: prepend option on 2nd index jquery 
Javascript :: javascript change color 
Javascript :: add new database mongodb 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =