RegExp Character Sets

Next ❯Character Classes

  • turned_in_notCharacter Sets
    • [abc]   : defines specific selection
    • [^abc] : defines other than specific selection
    • [a-c]    : defines range selection
    • [^a-c]  : defines other than range selection
  • To run below examples, try it inside <script> tag, in a basic html file


[abc]

It matches any character specified in the square brackets.
 
like [abc] : match any 'a' or 'b' or 'c'

For example :

var str="have a good day";
var pattern=/[agty]/g;
alert(str.match(pattern));
// Output: a,a,g,a,y

Matches: have a good day


[^abc]

It matches any character other then in the square brackets. Just add '^' at the beginning
like [^abc] : match any, other then a,b,c

For example :

var str="great day";
var pattern=/[^agty]/g;
alert(str.match(pattern));
// Output: r,e, ,d

Matches: great day


[a-c]

It matches any character within the range specified in the square brackets.

[abc] same as [a-c] in short
[0123456789] same as [0-9] in short

For example :

var str="great work";
var pattern=/[e-k]/g;
alert(str.match(pattern));
// Output: g,e,k

Matches: great work


[^a-c]

It matches any character other then, within the range in the square brackets.

[^abc] same as [^a-c] in short
[^0123456789] same as [^0-9] in short

For example :

var str="great work";
var pattern=/[^e-k]/g;
alert(str.match(pattern));
// Output: r,a,t, ,w,o,r

Matches: great work



Hello welcome back, abcd efgh ijkl mnopq rstuv wxyz ABCD EFGH IJKL MNOPQ RSTUV WXYZ 01234 56789 */\? +-$#= @!^&| _([{

/[abc]/g

abcaxzAXab09a-zA-Zb-k0-90-zA-zZ-aa-zA-ZaA\-Z

^abc^axzAX^ab09^a-z^A-Z^b-k^0-9^a-zA-Z^aA\^Z
  • Remember ! Inside square bracket [],every character act as individual, except ^ - \ (only when used at proper place)
  • trending_downBit Detailed

    Using an example:

    when we use /[ab]cd/ then it looks for
    possible matches : acd, bcd if found any, return the match
    no flag : found first match(return) then stop else return null

    when we use /[ab]cd/g then it looks for
    possible matches : acd, bcd if found any, return the match
    using 'g' flag : search whole string & returns all matches

    when we use /[ab]cd/ig then it looks for
    possible matches : acd, Acd, bcd, Bcd if found any, return the match
    using 'g' flag : search whole string & returns all matches
    using 'i' flag : just ignore cases


  • Character Classes
Next ❯Character Classes
TryOut Examples"Learn to Explore..!"

TryOut Editor

receipt