Node JS

Chapter 2 : JSETTING SKILLS

 JSETTING SKILLS PART 3

  1. Regular Expression
  2. Modules (Import & Export)

1. Regular Expression

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. 

1. Regular Expression

How to write a RegExp ?

/cat/gi;

Flags (optional )

Expression

Or

Pattern

/

/

1. Regular Expression

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

1. Regular Expression

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"]

1. Regular Expression

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"]

1. Regular Expression

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

1. Regular Expression

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"]

1. Regular Expression

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); // [" ", " ", " ", " ", " ", " ", " ", " ", " "]

1. Regular Expression

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"]

1. Regular Expression

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"]

1. Regular Expression

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"]

1. Regular Expression

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."]

1. Regular Expression

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"]

1. Regular Expression

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",...]

1. Regular Expression

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"}]

1. Regular Expression

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"}]

1. Regular Expression

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>"]

2. Modules (Import & Export)

Modules

The goal to use modules is to split your large code into smaller chunks of codes or files for better understanding and debugging .

2. Modules (Import & Export)

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() );

2. Modules (Import & Export)

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());

JSKILLS

Part 3

Ends

Node js Chapter 2 Part 3

By Youcef Madadi

Node js Chapter 2 Part 3

  • 281