Regex Cheat Sheet

less than 1 minute read

Author: Doe Hoon LEE

Regular Expression Cheat Sheet

C_

(1) Consecutive/Repeating pattern

// let str = "dd_aaa_vvvv_iiiii_dddddd";

return str.replace(/(.)\1+/g, "$1");

// => "d_a_v_i_d"

D_

(1) Match digits in a string

let str = "1A2BC3DEF4GDSAF84";
str.match(/\d+/g);
// => ["1", "2", "3", "4", "84"]

E_

(1) Every n(-th) letters

// cut every single letter
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".match(/.{1,1}/g);
// => ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

// cut every TWO letters
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".match(/.{1,2}/g);
// => ["AB", "CD", "EF", "GH", "IJ", "KL", "MN", "OP", "QR", "ST", "UV", "WX", "YZ"]

L_

(1) Match letters (uppercase in this example) in a string

let str = "1A2BC3DEF4GDSAF84";
str.match(/[A-Z]+/g);
// => ["A", "BC", "DEF", "GDSAF"]

N_

(1) Check if string contains numbers only

let str = "abc123";
let strTwo = "123";

/^\d+$/.test(str) // => false
/^\d+$/.test(strTwo) // => true

Tags:

Categories:

Updated:

Leave a comment