Skip to content

Instantly share code, notes, and snippets.

var name=" R e g E x p"
let namewithoutspaces=name.split(" ").join("");
console.log(namewithoutspaces); // "RegExp"
var name=" R e g E x p ";
var namewithoutspaces1=name.replace(" ",""); // "R e g E x p "
var namewithoutspaces2=name.replace(/\s/g,"");// "RegExp"
let re1=new RegExp("abc"); //output: /abc/
let re2=/abc/; //output: /abc/
re1.test("abcdefg") //true;
re2.test("abcdefg") //true;
let x="abc";
let re1=new RegExp(x); // output: /abc/
let re2=/x/; // output: /x/ won't work as a variable in literal notation.
re1.test("abcdefg"); //true
re2.test("abcdefg"); //false
var str="\n9 " /* "
9 " */
var regex=/\n\d\s/;
regex.test(str); //true;
var str="\n9 " /* "
9 " */
var regex=new RegExp("\n\\d\\s"); // /\n\d\s/
regex.test(str); //true;
var regex=/(?<circle>\d{2})(?<district>\d)[\s-]?(?<office>\d{3})/;
console.log(regex.exec("412207").groups); //{circle: "41", district: "2", office: "207"}
console.log(regex.exec("182-101").groups); //{circle: "18", district: "2", office: "101"}
console.log(regex.exec("224 123").groups); //{circle: "22", district: "4", office: "123"}
//Using two flags 'g' and 'm' together on a Regex.
var regex1= /^[0-9]{9,10}/gm; // /^[0-9]{9,10}/gm
var regex2= new RegExp("^[0-9]{9,10}","gm") // /^[0-9]{9,10}/gm
var regex=/\d/y;
regex.lastIndex=3;
regex.exec("0123")[0]; // returns '3' instead of '0' as the regex starts the search from the index 3 of string.
//Global Scope.
var globaldata={user:"xyz",loggedin:false,currentstate:"none"}
var userinput="Current State Info";
/* Impure way of handling login. Changing Global State,
operation is dependent upon global variable 'userinput',
no return value.
*/
var login=(globaldata)=>{
globaldata.loggedin=true;