This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var name=" R e g E x p" | |
let namewithoutspaces=name.split(" ").join(""); | |
console.log(namewithoutspaces); // "RegExp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let re1=new RegExp("abc"); //output: /abc/ | |
let re2=/abc/; //output: /abc/ | |
re1.test("abcdefg") //true; | |
re2.test("abcdefg") //true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str="\n9 " /* " | |
9 " */ | |
var regex=/\n\d\s/; | |
regex.test(str); //true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str="\n9 " /* " | |
9 " */ | |
var regex=new RegExp("\n\\d\\s"); // /\n\d\s/ | |
regex.test(str); //true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; |
OlderNewer