Youcef Madadi
Web and game development teacher
Chapter 2 : JSETTING SKILLS
What are Regular expressions ?
A regular expression is a sequence of characters that specifies a search pattern. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory.
How to write a RegExp ?
Flags (optional )
Expression
Or
Pattern
/
/
How to use RegExp ?
var regex = /quick/; // RegExp("quick")
var found = paragraph.match(regex);
console.log(found); // ["quick"]
var paragraph = 'The quick brown fox jumps over the lazy dog.\nIt barked.';
var regex = /the/; // RegExp("the")
var found = paragraph.search(regex);
console.log(found); // 31
Global match
var regex = /the/g; // RegExp("the","g")
var found = paragraph.match(regex);
console.log(found); // ["the"]
Case insensitive
var regex = /the/gi; // RegExp("the","gi")
var found = paragraph.match(regex);
console.log(found); // ["The", "the"]
Specific characters "[ ]"
var regex = /[abc]/g; // RegExp("[abc]","g")
var found = paragraph.match(regex);
console.log(found); // ["c", "b", "a", "b", "a"]
More specific characters "[ ]"
var regex = /[ft][ho][ex]/g;
var found = paragraph.match(regex);
console.log(found); // ["fox", "the"]
Specific characters with Range "[ - ]"
var regex = /[A-Z]/g; // RegExp("[A-Z]","g")
var found = paragraph.match(regex);
console.log(found); // ["T", "I"]
Exclude characters "[ ^ ]"
var regex = /[^fr]/g;
var found = paragraph.match(regex);
console.log(found.join("")); // null
Every charcters "."
var regex = /.o./g;
var found = paragraph.match(regex);
console.log(found); // ["row", "fox", " ov", "dog"]
Every alphanumeric "\w"
var regex = /\wo\w/g; // \W to get none alphanumericAl character
var found = paragraph.match(regex);
console.log(found); // ["row", "fox", "dog"]
Any numerical character "\d"
var regex = /\d/g; // \S to get none Numerical characters
var found = paragraph.match(regex);
console.log(found); // null
Any white space character "\s"
var regex = /\s/g; // \S to get none white space characters
var found = paragraph.match(regex);
console.log(found); // [" ", " ", " ", " ", " ", " ", " ", " ", " "]
Quantifiers (+) once or more
var regex = /X+D/g;
var found = "XXXXXD XXXXX D XDDDD".match(regex);
console.log(found); // ["XXXXXD", "XD"]
Quantifiers (*) none or many times
var regex = /X*D+/g;
var found = "XXXXXD XXXXX D XDDDD".match(regex);
console.log(found); // ["XXXXXD", "D", "XDDDD"]
Quantifiers (?) doesn't have to exist
var regex = /colou?r/g;
var found = "color colour collar".match(regex);
console.log(found); // ["color", "colour"]
Quantifiers ({number}) exist number of times
var regex = /\wo{2}\w/g;
var found = "food good dude mood rude".match(regex);
console.log(found); // ["food", "good", "mood"]
Quantifiers ({min,max}) exist in a set of interval
var regex = /XD{3,4}/g;
var found = "XDDD XDDDDDDD XD XDDDD".match(regex);
console.log(found); // ["XDDD", "XDDDD", "XDDDD"]
Quantifiers ({min,}) exist at least "min" times
var regex = /XD{3,}/g;
var found = "XDDD XDDDDDDD XD XDDDD".match(regex);
console.log(found); // ["XDDD", "XDDDDDDD", "XDDDD"]
Get first words
var regex = /^\w+/mg;
var found = paragraph.match(regex);
console.log(found); // ["The","It"]
Get last words
var regex = /\S+$/mg;
var found = paragraph.match(regex);
console.log(found); // ["dog.", "barked."]
Lookahead assertion
var regex = /fo(?=x)/g;
var found = paragraph.match(regex);
console.log(found); // ["fo"]
Negative lookahead assertion
var regex = /XD{1,4}(?!D)/g;
var found = "XDDD XDDDDDDD XD XDDDD".match(regex);
console.log(found); // ["XDDD", "XD", "XDDDD"]
Lookbehind assertion
var regex = /(?<=f)\w+/g;
var found = paragraph.match(regex);
console.log(found); // ["ox"]
Negative Lookbehind assertion
var regex = /(?<!\s)\w+/g;
var found = paragraph.match(regex);
console.log(found); // ["The", "uick", "rown",...]
Groups "( )"
var regex = /\w*(bro)\w*/;
var found = paragraph.match(regex);
console.log(found); // ["brown", "bro", index: 10, length: 2]
Groups with names"(?<name> )"
var regex = /(?<animal>fox)/;
var found = paragraph.match(regex);
console.log(found); // [0: "fox", 1: "fox", groups: {animal: "fox"}]
Or logic "|"
var regex = /fox|dog/g;
var found = paragraph.match(regex);
console.log(found); // ["fox", "dog"]
Groups "Or" with names"(?<name> a|b )"
var regex = /(?<animal>fox|dog)/g;
var found = paragraph.matchAll(regex);
console.log([...found]); // ["fox", "fox", groups: {animal: "fox"}]
// ["dog", "dog", groups: {animal: "dog"}]
Greedy quantifiers (* or +)
var paragraph=`<div> <a href="https://google.com">google</a> </div>`
var regex = /<.+>/g;
var found = paragraph.match(regex);
console.log(found); // ["<div> <a href="https://google.com">google</a> </div>"]
Stopping greedy quantifiers using ?
var regex = /<.+?>/g;
var found = paragraph.match(regex);
console.log(found); //["<div>", "<a href="https://google.com">", "</a>", "</div>"]
The goal to use modules is to split your large code into smaller chunks of codes or files for better understanding and debugging .
Export using "module.exports"
// export.js
var call = function () {
return "called";
};
module.exports = call;
Import using "require()"
// import.js
const call = require("export.js");
console.log( call() );
Export using "export"
// export.mjs
var call = function () {
return "called";
};
export default call;
Import using "import"
// import.mjs
import call from "./export.mjs";
console.log(call());
By Youcef Madadi