Regular Expression

A Regular Expression (RegEx, regex, regexp, RE, etc.) is a way to search through strings. It helps in doing validation, getting/matching certain pieces of a string, finding and replacing strings and more.

RegEx Format

Built-In JavaScript Functions

Resources

Examples

  1. re: /at/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    re.test(str): true

    str.match(re):
    at
    at
    at

  2. re: /at/

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    re.test(str): true

    str.match(re):
    at

  3. re: /e+/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: As many occurrences of 'e', with each instance having zero or more consecutive 'e's. Valid sub strings: '', 'e', 'ee', 'eee', 'eeee', ...

    re.test(str): true

    str.match(re):
    e
    e
    ee
    e
    e
    e

  4. re: /e+a/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Explanation of re3 + occurrence of 'a' compulsory at end

    re.test(str): true

    str.match(re):
    ea
    ea

  5. re: /e+a?/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Explanation of re3 + '?' indicates that the char just before it ('a' in this case) is optional (0 or 1 occurrence or 'a').

    re.test(str): true

    str.match(re):
    e
    e
    ee
    ea
    e
    ea

  6. re: /re*/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: One 'r', optionally followed by any number of 'e's (0 or multiple occurrences of 'e')

    re.test(str): true

    str.match(re):
    r
    ree
    r
    r

  7. re: /t../g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: 't' followed by any two char [other than '\n' (new line) - it won't match it (check the o/p)]

    re.test(str): true

    str.match(re):
    t c
    t r
    the
    tre
    t w
    to

  8. re: /..\./g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Escape the usual meaning of a period ('.') in regex with a '' and treat that period like a normal period

    re.test(str): true

    str.match(re):
    et.
    at.

  9. re: /\w/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match every word character (alphanumeric and underscore)

    re.test(str): true

    str.match(re):
    T
    h
    e
    f
    a
    t
    c
    a
    t
    r
    a
    n
    d
    o
    w
    n
    t
    h
    e
    S
    t
    r
    e
    e
    t
    I
    t
    w
    a
    s
    s
    e
    a
    r
    c
    h
    i
    n
    g
    f
    o
    r
    a
    m
    o
    u
    s
    e
    t
    o
    e
    a
    t

  10. re: /\W/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match every non-word character (ie, chars which are NOT alphanumeric and underscore)

    re.test(str): true

    str.match(re):






    .
    \n







    .

  11. re: /\s/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match every whitespace char

    re.test(str): true

    str.match(re):






    \n






  12. re: /\S/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match every char that is NOT a whitespace

    re.test(str): true

    str.match(re):
    T
    h
    e
    f
    a
    t
    c
    a
    t
    r
    a
    n
    d
    o
    w
    n
    t
    h
    e
    S
    t
    r
    e
    e
    t
    .
    I
    t
    w
    a
    s
    s
    e
    a
    r
    c
    h
    i
    n
    g
    f
    o
    r
    a
    m
    o
    u
    s
    e
    t
    o
    e
    a
    t
    .

  13. re: /\w{4}/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match every word character (alphanumeric and underscore) sub-str which is 4 in length.

    re.test(str): true

    str.match(re):
    down
    Stre
    sear
    chin
    mous

  14. re: /\w{4,6}/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match every word character (alphanumeric and underscore) sub-str which is min 4 in length and max 6.

    re.test(str): true

    str.match(re):
    down
    Street
    search
    mouse

  15. re: /\w{4,}/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match every word character (alphanumeric and underscore) sub-str which is 4 or more in length.

    re.test(str): true

    str.match(re):
    down
    Street
    searching
    mouse

  16. re: /[ae]t/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match any sub-str of length 2 starting with either 'a' or 'e' and ending in 't'

    re.test(str): true

    str.match(re):
    at
    at
    et
    at

  17. re: /[a-zA-Z]t/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match any sub-str with length 2 starting with any char between 'a' and 'z' or 'A' and 'Z' (both inclusive) and ending in 't' (FYI: In '/[^a-zA-Z]t/g', the caret '^' negates the entire set (ie, the stuff between '[]'))

    re.test(str): true

    str.match(re):
    at
    at
    St
    et
    It
    at

  18. re: /[0-9]t/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match any sub-str with length 2 starting with any number between '0' and '9' (both inclusive) and ending in 't'

    re.test(str): false

    str.match(re):

  19. re: /(t|T)he/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: '()' allows the making of an isolated group. Here, it means look for 'the' or 'The', same as '[tT]he'.

    re.test(str): true

    str.match(re):
    The
    the

  20. re: /(re){2,3}/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: 're{2,3}' will only affect 'e', ie, match sub-strs with one occurrence of 'r' followed by min 2 and max 3 consecutive occurrences of 'e', but '(re){2,3}' will match sub-strs with 're' occurring together consecutively for min 2 and max 3 times in a str, eg: 'rere', '<rerere>re' (only first 3 are matched out of the 4 consecutive repetitions of 're').

    re.test(str): false

    str.match(re):

  21. re: /(t|r|e){2,3}/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Explanation of 're19' + match any sub-str of length 2 or 3 which contain a combo of 't', 'r' or 'e'.

    re.test(str): true

    str.match(re):
    tre
    et

  22. re: /(t|r|e){2,3}\./g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Explanation of 're21' + match a '.' after the 2 or 3 length sub-str.

    re.test(str): true

    str.match(re):
    eet.

  23. re: /^I/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match the beginning of the str

    re.test(str): false

    str.match(re):

  24. re: /^I/gm

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match the beginning of every new line

    re.test(str): true

    str.match(re):
    I

  25. re: /\.$/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match the end of the str

    re.test(str): true

    str.match(re):
    .

  26. re: /\.$/gm

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Match the end of every new line

    re.test(str): true

    str.match(re):
    .
    .

  27. re: /(?<=[tT]he)./g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Positive look behind. '<' indicates a look behind and '=' indicates a positive look behind. This exp gives any char after 'the' or 'The', without including them in the result.

    re.test(str): true

    str.match(re):

  28. re: /(?

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Negative look behind. '<' indicates a look behind and '!' indicates a negative look behind. This exp gives any char that is not preceded by 'the' or 'The', without including the two words as sub-strs in the result.

    re.test(str): true

    str.match(re):
    T
    h
    e
    f
    a
    t

    c
    a
    t

    r
    a
    n

    d
    o
    w
    n

    t
    h
    e
    S
    t
    r
    e
    e
    t
    .
    I
    t

    w
    a
    s

    s
    e
    a
    r
    c
    h
    i
    n
    g

    f
    o
    r

    a

    m
    o
    u
    s
    e

    t
    o

    e
    a
    t
    .

  29. re: /.(?=at)/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Positive look ahead. '=' indicates a positive look ahead. This exp gives any char which is followed by 'at', without including 'at' in the result.

    re.test(str): true

    str.match(re):
    f
    c
    e

  30. re: /.(?!at)/g

    str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.

    NOTE: Negative look ahead. '!' indicates a negative look ahead. This exp gives any char which is NOT followed by 'at', without including 'at' as a sub-str in the result.

    re.test(str): true

    str.match(re):
    T
    h
    e

    a
    t

    a
    t

    r
    a
    n

    d
    o
    w
    n

    t
    h
    e

    S
    t
    r
    e
    e
    t
    .
    I
    t

    w
    a
    s

    s
    e
    a
    r
    c
    h
    i
    n
    g

    f
    o
    r

    a

    m
    o
    u
    s
    e

    t
    o

    a
    t
    .

  31. re: /(?:(\+[0-9][0-9]?)[- ]?)?\(?(?\d{3})\)?[ -]?(\d{3})[- ]?(\d{4})/gm

    str: 1234567890\n123-456-7890\n123 456 7890\n(123) 456-7890\n+1 123 456 7890\n+91-(123)-456-7890

    NOTE: Phone number matching

    re.test(str): true

    str.match(re):
    1234567890
    123-456-7890
    123 456 7890
    (123) 456-7890
    +1 123 456 7890
    +91-(123)-456-7890

    str.replace(re, "$1$$3$4"): 1234567890\n1234567890\n1234567890\n1234567890\n+11234567890\n+911234567890

    '()' in re31 is for capturing groups. They can be referenced as follows '$'. They can be named as well '(?RE)'. More at javascript.info/regexp-groups. '?:' makes a group non-capturing, ie, it can't be referenced with a '$' (doesn't apply to groups within it)

  32. re: /(\S{1,})@(\S{1,})\.(\S{1,})/gm

    str: harshgkapadia@gmail.com\nharsh-g-kapadia@gmail.com\nharsh.kapadia@g-mail.com

    NOTE: e-mail matching

    re.test(str): true

    str.match(re):
    harshgkapadia@gmail.com
    harsh-g-kapadia@gmail.com
    harsh.kapadia@g-mail.com

    str.replace(re, "$1@$2.$3"): harshgkapadia@gmail.com\nharsh-g-kapadia@gmail.com\nharsh.kapadia@g-mail.com