Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Regular Expressions

JS Regular Expressions

A regular expression is a sequence of characters that forms a pattern.

The pattern is used for searching and replacing characters in strings.

The RegExp Object is a regular expression with added Properties and Methods.

Syntax
					 
        
          /pattern/modifier(s);
        
      
Example
					 
        
          let pattern = /lynxsia/i;
        
      

/lynxsia/i is a regular expression.

lynxsia is a pattern (to be used in a search).

i is a modifier (modifies the search to be case-insensitive).

Modifiers

Modifiers are used to perform case-insensitive and global searches.

Modifier Description
g Perform a global match (find all matches rather than stopping after the first match)
i Perform case-insensitive matching
m Perform multiline matching
Brackets

Brackets are used to find a range of characters.

Expression Description
[abc] Find any character between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find any character between the brackets (any digit)
[^0-9] Find any character NOT between the brackets (any non-digit)
(x|y) Find any of the alternatives separated with |
Meta Characters

Meta characters are characters with a special meaning.

Metacharacter Description
. Find a single character, except newline or line terminator
\w Find a word character
\W Find a non-word character
\d Find a digit
\D Find a non-digit character
\s Find a whitespace character
\S Find a non-whitespace character
\b Find a match at the beginning/end of a word, beginning like this: \bHI, end like this: HI\b
\B Find a match, but not at the beginning/end of a word
\0 Find a NULL character
\n Find a new line character
\f Find a form feed character
\r Find a carriage return character
\t Find a tab character
\v Find a vertical tab character
\xxx Find the character specified by an octal number xxx
\xdd Find the character specified by a hexadecimal number dd
\udddd Find the Unicode character specified by a hexadecimal number dddd
Quantifiers

Quantifiers define quantities.

Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n{X} Matches any string that contains a sequence of X n's
n{X,Y} Matches any string that contains a sequence of X to Y n's
n{X,} Matches any string that contains a sequence of at least X n's
n$ Matches any string with n at the end of it
^n Matches any string with n at the beginning of it
?=n Matches any string that is followed by a specific string n
?!n Matches any string that is not followed by a specific string n
Regex Search

The search() method searches a string for a specified value and returns the position of the match.

					 
        
          let text = "Lynxsia IT Solutions";
          text.search("IT"); // 8
          text.search("it"); // -1
        
      
Regex Replace

The replace() method returns a modified string where the pattern is replaced.

					 
        
          let text = "Lynxsia IT Solution";
          text.replace("Solution","Solutions");

          // OR

          text.replace(/Solution/i,"Solutions");
        
      
Some Useful Regex Expressions
Username

Match a username (string) allowing only letters, numbers, and underscores.

					 
        
          let usernameRegex = /^\w+$/;
        
      
Email

Match a valid email.

					 
        
          let emailRegex = /^\w+([.-]?\w+([.-]?\w+)*(\.\w{2,})+$/;
        
      
Password

Match a password with at least 8 characters long and contains at least one uppercase letter, one lowercase letter, one number, and one symbol (#, ?, !, @, $, %, ^, &, *, -)

					 
        
          let passwordRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,20}$/;
        
      
Date

Match a date of the format yyyy-mm-dd.

					 
        
          let dateRegex = /^\d{4}-\d{2}-\d{2}$/;
        
      
Time

Match a time of the format hh:mm:ss

					 
        
          let timeRegex = /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/;
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved