Learn Regular Expressions, aka regex or regexp, by inspecting the various examples.
- Regular Expressions are Objects.
- There are two ways to create regex Objects
const regex1 = new RegExp(“hello”);
const regex2 = /hello/;
- Once you have a regex object, you can use the one one of the methods on regex constructor like test(), exec(), etc.
test()
returs true if pattern is found in the passed string; false if not.exec()
returns an array of matches.toString()
returns a string of the regular expression syntax.
match()
returns an array of matches just like exec on regex.search()
returns an index of the matched string.replace()
replaces matches with a string.split()
splits a string into an array. The division is based on the regular expression pattern.
- It can be used as
/pattern/flags;
ornew RegExp("pattern”, "flags");
g
- global, match more than one occurance.i
- case insensitive match, upper or lower case doesn’t matter.m
- multi-line match.
var string = "The dog chased the cat";
var regex = /the/;
var result = regex.test(string);
console.log(result); // true
var string = "Hello World";
var regex = /Hello/;
var result = regex.test(string);
console.log(result); // true
var string = "hello World";
var regex = /Hello/;
var result = regex.test(string);
console.log(result); // false
var string = "Jhonny has cat and dog.";
var regex = /cat|dog|bird/;
var result = regex.test(string);
console.log(result); // true
var string = "davidBeckham";
var regex = /davidbeckham/i;
var result = regex.test(string);
console.log(result); // true
var string = "Extract the word 'coding' form this string";
var regex = /coding/;
var result = string.match(regex);
console.log(result); // ["coding", index: 18, input: "Extract the word 'coding' form this string 'coding'", groups: undefined]
var string = "Repeat, Repeat, Repeat";
var regex = /Repeat/g;
var result = string.match(regex);
console.log(result); // ["Repeat", "Repeat", "Repeat"]
var string = "Repeat, repeat, repeat";
var regex = /repeat/ig;
var result = string.match(regex);
console.log(result); // ["Repeat", "repeat", "repeat"]
var string = "release, repeat, revert";
var regex = /re./g;
var result = string.match(regex);
console.log(result); // ["rel", "rep", "rev"]
var string = "fun, sun, run";
var regex = /.un/g;
var result = string.match(regex);
console.log(result); // ["fun", "sun", "run"]
var string = "how is that so hot?";
var regex = /h.t/g;
var result = string.match(regex);
console.log(result); // ["hat", "hot"]
var string = "bag, big, bug";
var regex = /b[aiu]g/g;
var result = string.match(regex);
console.log(result); // ["bag", "big", "bug"]
var string = "Beware of bugs in the abOve code.";
var regex = /[aeiou]/g;
var result = string.match(regex);
console.log(result); // ["e", "a", "e", "o", "u", "i", "e", "a", "o", "e", "o", "e"]
var string = "Beware of bUgs in the abOve code.";
var regex = /[aeiou]/ig;
var result = string.match(regex);
console.log(result); // ["e", "a", "e", "o", "U", "i", "e", "a", "O", "e", "o", "e"]
var string = "The quick brown fox jumps over the lazy dog.";
var regex = /[a-z]/ig;
var result = string.match(regex);
console.log(result); // ["T", "h", "e", "q", "u", "i", "c", "k", "b", "r", "o", "w", "n", "f", "o", "x", "j", "u", "m", "p", "s", "o", "v", "e", "r", "t", "h", "e", "l", "a", "z", "y", "d", "o", "g"]
var string = "Number pi is 3.14159265359.";
var regex = /[2-6h-s]/ig;
var result = string.match(regex);
console.log(result); // ["N", "m", "r", "p", "i", "i", "s", "3", "4", "5", "2", "6", "5", "3", "5"]
var string = "3 blind mice.";
var regex = /[^0-9aeiou]/ig;
var result = string.match(regex);
console.log(result); // [" ", "b", "l", "n", "d", " ", "m", "c", "."]
var string = "Mississippi";
var regex = /s+/g;
var result = string.match(regex);
console.log(result); // ["ss", "ss"]
var string = "Mississippi is a state of the United States.";
var regex = /s+/g;
var result = string.match(regex);
console.log(result); // ["ss", "ss", "s", "s", "s"]
var string = "gooooooooal!";
var regex = /go*/;
var result = string.match(regex);
console.log(result1); // ["goooooooo", index: 0, input: "gooooooooal!", groups: undefined]
var string = "feels good";
var regex = /go*/;
var result = string.match(regex);
console.log(result); // ["goo", index: 6, input: "feels good", groups: undefined]
var string = "over the moon";
var regex = /go*/;
var result = string.match(regex);
console.log(result); // null
var string = "Aaaaarrghhh!";
var regex = /Aa*/;
var result = string.match(regex);
console.log(result); // ["Aaaaa", index: 0, input: "Aaaaarrghhh!", groups: undefined]
var string = "titanic";
var regex = /t[a-z]*i/;
var result = string.match(regex);
console.log(result); // ["titani", index: 0, input: "titanic", groups: undefined]
var string = "titanic";
var regex = /t[a-z]*?i/;
var result = string.match(regex);
console.log(result); // ["ti", index: 0, input: "titanic", groups: undefined]
var string = "<h1>Winter is comming</h1>";
var regex = /<.*>/;
var result = string.match(regex);
console.log(result); // ["<h1>Winter is comming</h1>", index: 0, input: "<h1>Winter is comming</h1>", groups: undefined]
var string = "<h1>Winter is comming</h1>";
var regex = /<.*?>/;
var result = string.match(regex);
console.log(result); // ["<h1>", index: 0, input: "<h1>Winter is comming</h1>", groups: undefined]
var string = "P1P2P3P4P4P5CCCP7P8P9";
var regex = /C+/;
var result = string.match(regex);
console.log(result); // ["CCC", index: 12, input: "P1P2P3P4P4P5CCCP7P8P9", groups: undefined]
var string = "John and Ricky Both like racing";
var regex = /^John/;
var result = regex.test(string);
console.log(result); // true
var string = "Ricky and John Both like racing";
var regex = /^John/;
var result = regex.test(string);
console.log(result); // false
var string = "John loves Emma";
var regex = /Emma$/;
var result = regex.test(string);
console.log(result); // true
var string = "Emma loves John";
var regex = /Emma$/;
var result = regex.test(string);
console.log(result); // false
var string = "There are 5 crates.";
var regex = /\w/g;
var result = string.match(regex).length;
console.log(result); // 15
var string = "Thereare5crates";
var regex = /\w/g;
var result = string.match(regex).length;
console.log(result); // 15
var string = "There are 5 crates.";
var regex = /\W/g;
var result = string.match(regex);
console.log(result); // [" ", " ", " ", "."]
var string = "Thereare5crates";
var regex = /\W/g;
var result = string.match(regex);
console.log(result); // null
var string = "Your burger will be $5.00";
var regex = /\d/g;
var result = string.match(regex);
console.log(result); // ["5", "0", "0"]
var string = "Your burger will be $5.00";
var regex = /\D/g;
var result = string.match(regex);
console.log(result); // ["Y", "o", "u", "r", " ", "b", "u", "r", "g", "e", "r", " ", "w", "i", "l", "l", " ", "b", "e", " ", "$", "."]
- If there are numbers, they must be at the end.
- Letters can be lowercase and uppercase
- At least two letters long.
var string = "Jack1";
var regex = /^[A-Za-z]{2,}\d*$/;
var result = regex.test(string);
console.log(result); // true
var string = "Jack";
var regex = /^[A-Za-z]{2,}\d*$/;
var result = regex.test(string);
console.log(result); // true
var string = "jo1";
var regex = /^[A-Za-z]{2,}\d*$/;
var result = regex.test(string);
console.log(result); // true
var string = "j2123";
var regex = /^[A-Za-z]{2,}\d*$/;
var result = regex.test(string);
console.log(result); // false
var string = "ja23ck1";
var regex = /^[A-Za-z]{2,}\d*$/;
var result = regex.test(string);
console.log(result); // false
var string = "Whitespace is important.";
var regex = / /g;
var result = string.match(regex);
console.log(result); // [" ", " "]
var string = "Whitespace is important.";
var regex = /\s/g;
var result = string.match(regex);
console.log(result); // [" ", " "]
var string = "s p a c e";
var regex = /[^ ]/g;
var result = string.match(regex);
console.log(result); // ["s", "p", "a", "c", "e"]
var string = "s p a c e";
var regex = /\S/g;
var result = string.match(regex);
console.log(result); // ["s", "p", "a", "c", "e"]
var string = "Ohhh no";
var regex = /Oh{3,6} no/;
var result = regex.test(string);
console.log(result); //true
var string = "Ohhh no";
var regex = /Oh{3,} no/;
var result = regex.test(string);
console.log(result); // true
var string = "Ohhh no";
var regex = /Oh{4,6} no/;
var result = regex.test(string);
console.log(result); // false
var string = "Hazzzzah";
var regex = /z{4,}/;
var result = regex.test(string);
console.log(result); // true
var string = "Timmmmber";
var regex = /Tim{4}ber/;
var result = regex.test(string);
console.log(result); // true
var string = "favorite";
var regex = /favou?rite/;
var result = regex.test(string);
console.log(result); // true
var string = "favourite";
var regex = /favou?rite/;
var result = regex.test(string);
console.log(result); // true
var string = "fatihturgut1";
var regex = /fatihturgut(?=1)/;
var result = string.match(regex);
console.log(result); // ["fatihturgut", index: 0, input: "fatihturgut1", groups: undefined]
var string = "fatihturgut2";
var regex = /fatihturgut(?=1)/;
var result = string.match(regex);
console.log(result); // null
var string = "fatihturgut1";
var regex = /fatihturgut(?!2)/;
var result = string.match(regex);
console.log(result); // ["fatihturgut", index: 0, input: "fatihturgut1", groups: undefined]
var string = "fatihturgut1";
var regex = /fatihturgut(?!1)/;
var result = string.match(regex);
console.log(result); // null
var string = "The sky is silver.";
var regex = /silver/;
var result = string.replace(regex, "blue");
console.log(result); // The sky is blue.
var string = "Code Camp";
var regex = /(\w+)\s(w+)/;
var result = string.replace(regex, "$1 $2");
console.log(result); // Code Camp.
var string = "This sandwich is good.";
var regex = /good/;
var result = string.replace(regex, "okey-dokey");
console.log(result);
var string = " Hello World! ";
var regex = /^\s+|\s+$/g;
var result = string.replace(regex, "");
console.log(result); // Hello World!
This project is licensed under the GNU General Public License v2.0 - see the LICENSE file for details