Regular Expressions
This page is under construction. Please come back later.
#!/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/))); console.log("Match 2: " + Utils.defined(s.match(/\bs.*e\b/))); console.log("Match 3: " + Utils.defined(s.match(/s...e/))); console.log("Match 4: " + Utils.defined(s.match(/b.d/))); let subgroups = Utils.match(/(\w+)\s*(\w+)/, s); console.log("Find First (with subgroups): " + Utils.listToString(subgroups)); subgroups = Utils.match(/bad match/, s); console.log("Find First (bad match): " + Utils.listToString(subgroups)); let matches = Utils.match(/\w+/g, s); console.log("Find All: (matches only)" + Utils.listToString(matches)); matches = Utils.match(/bad match/g, s); console.log("Find All (bad match): " + Utils.listToString(matches)); } 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): []
#!/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(/\.\.\./, "!!!")); console.log("Replace First 2: " + s.replace(/\b...\b/, "???")); console.log("Replace First 3: " + s.replace(/b.d/, "???")); console.log("Replace First 4: " + s.replace(/(\w+) (\w+)/, "$2 $1")); console.log("Replace All 1: " + s.replaceAll(/\b...\b/g, "???")); console.log("Replace All 2: " + s.replaceAll(/(\w+) (\w+)/g, "$2 $1")); } 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...
#!/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(/ /))); console.log("Split 2: " + Utils.listToString(str.split(/[eo]/))); console.log("Split 3: " + Utils.listToString(str.split(/\s/))); console.log("Split 4: " + Utils.listToString(str.split(/\W/))); } 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", "", "", ""]
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[JavaScript Language Reference]], Mozilla Developer Network
- [[Mozilla Developer Network]]
- Download [[node.js]]
- [[w3schools.com]]