function assert(condition, message) {
if (!condition) {
throw new Error(message || "Assertion failed");
}
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
var a = "hello. how are you?";
var escapedA = escapeRegExp(a);
console.log(escapedA);
var regex = new RegExp(escapedA);
var result = regex.test(a);
console.log(result);
assert(result, "The regex should match the original string");
var re = /ab+c/;
console.log(re);
var myRe = new RegExp("d(b+)d", "g");
var myArray = myRe.exec("cdbbdbsbz");
console.log(myArray);
var myRe = /d(b+)d/g;
var myArray = myRe.exec("cdbbdbsbz");
console.log(myArray);
var myArray = /d(b+)d/g.exec("cdbbdbsbz");
console.log(myArray);
var expectedArray = "cdbbdbsbz".match(/d(b+)d/g);
console.log(expectedArray);
var myRe = /d(b+)d/g;
var myArray = myRe.exec("cdbbdbsbz");
console.log(myRe.lastIndex); console.log("The value of lastIndex is " + myRe.lastIndex);
var myArray = /d(b+)d/g.exec("cdbbdbsbz");
console.log("The value of lastIndex is " + /d(b+)d/g.lastIndex);
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
console.log(newstr);
var re = /\w+\s/g;
var str = "fee fi fo fum";
var myArray = str.match(re);
console.log(myArray);
var re_01 = /\w+\s/g;
var re_02 = new RegExp("\\w+\\s", "g");
assert(re_01.source === re_02.source, "RegExp source properties should match");
var xArray;
var str2 = "fee fi fo fum";
var re_03 = new RegExp("\\w+\\s", "g");
while ((xArray = re_03.exec(str2))) {
console.log(xArray, xArray.index, `lastIndex of regexp: ${re_03.lastIndex}`);
}
result