regular expression

JavaScript Regular expression for beginner

Today I explore with Javascript regular expression. It is interesting. I have added below what I learned so far.

You have to put regular expression syntax is of two slashes. Like this /hello/. if you include i after that it will escape case sensitivity. for example /hello/i.

Here is some function that works on regular expression:

  1. exec() : Return result in an array or null
  2. test() : Retun result true or false
  3. match() : Its works same as exec(). dffterent between exec and match is exec show result on only first match found and match shows all of them.
  4. search(): Return index of the first match if not found return -1

let re;

re = /hello/;

re = /hello/i;

const content = 'Hello there, is it 2449? Bangladesh';

re.test(content); // const resultContent =


How it works:

  • /^h/i < search start with character h
  • /wrold$/ : the content must end with world world
  • /^hello$/ : The word must begin and end with hello
  • /h.llo/ : It will matches any one character.
  • /h*llo/ : It will matches any charter 0 or more time on the content.
  • /gre?a?y/ : Its works as optional if any word has a or e it will work on that.
  • /gre?a?y\?/ : The backslash use for escape optional.

Use of Brackets on Regular expression:

  • /gr[ae]y/ : It will match same as before any one character. like a or e.
  • /[GF]ray/ : must be a G or F
  • /^[GH]/ : match anything except G and F
  • /[A-Z]/ : match any uppercase letter
  • /[a-z]/ : match any lowercase letter
  • /[A-Za-z]/ : match any letter.
  • /[0-9]/ : match any digit.

Quantifiers on Regular Expression:

Type a letter more than one or a custom time we use qualifiers. here is how to use that:

  • /hel{2}o/ : Must l need to occur exactly {mentioned} amount of time
  • /hel{2,4}o/ : Must l need to occur 2-4 amount of time. more than that or less than that will not work.
  • /hell{2,}o/ : in this case, l can occur 2 to an infinite amount of time.
  • /^([0-9]x){3}/ : this grouping . if i want 3x3x3x than this will work fine.

Regular expression shorthand character classes

  • /\w/ : will support alphabetic and numeric or _ characters. only the first character will match.
  • /\w+/ : It Will add one more letter. All the characters will show.
  • /\W/ : Used in for non words letter.
  • /\d/ : It will match only numbers only. no characters will match on this.
  • /\d+/ : If u want to show one or more letters this will work fine.
  • /\D/ : numbers will not match on this. only character.
  • /\s/ : Count space as a character.
  • /\S/ : non-whitespace character will show. whitespace will not count.
  • /hell\b/ : word boundary. If you want to search full word, not a 2-3 charter match uses this. For example, “Hello, I am at hell”. in this case it will escape ‘hello’ and count the ‘hell’.
  • /x(?=y)/ : assertions work as conditions. for this, it will only match if the character found ‘xy’.
  • /x(?!y)/ : assertions work as conditions. for this, it will only match if the character found that not followed by ‘xy’.

I hope you enjoy this post. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *