Pure Programmer
Blue Matrix


Cluster Map

Regular Expressions

L1

This page is under construction. Please come back later.

RegEx1.js
#!/usr/bin/env node;
const Utils = require('./Utils');

const main = async () => {
	const s = "Four score and seven years ago...";
	console.log(["Match 1: ", Utils.defined(s.match(/s.*e/))].join(''));
	console.log(["Match 2: ", Utils.defined(s.match(/\bs.*e\b/))].join(''));
	console.log(["Match 3: ", Utils.defined(s.match(/s...e/))].join(''));
	console.log(["Match 4: ", Utils.defined(s.match(/b.d/))].join(''));

	let subgroups = Utils.match(/(\w+)\s*(\w+)/, s);
	console.log(["Find First (with subgroups): ", Utils.listToString(subgroups)].join(''));
	subgroups = Utils.match(/bad match/, s);
	console.log(["Find First (bad match): ", Utils.listToString(subgroups)].join(''));

	let matches = Utils.match(/\w+/g, s);
	console.log(["Find All: (matches only)", Utils.listToString(matches)].join(''));
	matches = Utils.match(/bad match/g, s);
	console.log(["Find All (bad match): ", Utils.listToString(matches)].join(''));
}
main().catch( e => { console.error(e) } );

Output
$ node RegEx1.js Match 1: true Match 2: true Match 3: true Match 4: false Find First (with subgroups): ["Four score", "Four", "score"] Find First (bad match): [] Find All: (matches only)["Four", "score", "and", "seven", "years", "ago"] Find All (bad match): []
RegEx2.js
#!/usr/bin/env node;
const Utils = require('./Utils');

const main = async () => {
	const s = "Four score and seven years ago...";
	console.log(["Replace First 1: ", s.replace(/\.\.\./, "!!!")].join(''));
	console.log(["Replace First 2: ", s.replace(/\b...\b/, "???")].join(''));
	console.log(["Replace First 3: ", s.replace(/b.d/, "???")].join(''));
	console.log(["Replace First 4: ", s.replace(/(\w+) (\w+)/, "$2 $1")].join(''));

	console.log(["Replace All 1: ", s.replaceAll(/\b...\b/g, "???")].join(''));
	console.log(["Replace All 2: ", s.replaceAll(/(\w+) (\w+)/g, "$2 $1")].join(''));
}
main().catch( e => { console.error(e) } );

Output
$ node RegEx2.js Replace First 1: Four score and seven years ago!!! Replace First 2: Four score ??? seven years ago... Replace First 3: Four score and seven years ago... Replace First 4: score Four and seven years ago... Replace All 1: Four score ??? seven years ???... Replace All 2: score Four seven and ago years...
RegEx3.js
#!/usr/bin/env node;
const Utils = require('./Utils');

const main = async () => {
	const STR = "Four score and seven years ago...";
	console.log(["Split 1: ", Utils.listToString(STR.split(/ /))].join(''));
	console.log(["Split 2: ", Utils.listToString(STR.split(/[eo]/))].join(''));
	console.log(["Split 3: ", Utils.listToString(STR.split(/\s/))].join(''));
	console.log(["Split 4: ", Utils.listToString(STR.split(/\W/))].join(''));
}
main().catch( e => { console.error(e) } );

Output
$ node RegEx3.js Split 1: ["Four", "score", "and", "seven", "years", "ago..."] Split 2: ["F", "ur sc", "r", " and s", "v", "n y", "ars ag", "..."] Split 3: ["Four", "score", "and", "seven", "years", "ago..."] Split 4: ["Four", "score", "and", "seven", "years", "ago", "", "", ""]
javascript

Questions

Projects

More ★'s indicate higher difficulty level.

References