JavaScript RegExp Objects
Working with Regular Expression objects and their properties
🔧 What are RegExp Objects?
RegExp objects are JavaScript objects that represent regular expressions. They have properties and methods that help you work with pattern matching and provide information about the regex.
// Creating RegExp objects
let regex1 = /hello/gi;
let regex2 = new RegExp("hello", "gi");
console.log(regex1.source); // "hello"
console.log(regex1.flags); // "gi"
Output:
gi
RegExp Object Properties
source
The pattern text of the regex
regex.source
flags
The flags used in the regex
regex.flags
global
Whether 'g' flag is set
regex.global
lastIndex
Index for next match start
regex.lastIndex
🔹 Creating RegExp Objects
Two ways to create RegExp objects:
// Method 1: Literal notation
let regex1 = /pattern/flags;
// Method 2: Constructor
let regex2 = new RegExp("pattern", "flags");
// Examples
let emailRegex = /\w+@\w+\.\w+/gi;
let phoneRegex = new RegExp("\\d{3}-\\d{3}-\\d{4}", "g");
console.log("Email regex:", emailRegex);
console.log("Phone regex:", phoneRegex);
console.log("Are they equal?", emailRegex === phoneRegex);
Output:
Phone regex: /\d{3}-\d{3}-\d{4}/g
Are they equal? false
🔹 RegExp Properties in Action
Exploring RegExp object properties:
// Create a RegExp with various flags
let regex = /hello/gim;
console.log("Pattern source:", regex.source);
console.log("All flags:", regex.flags);
console.log("Global flag:", regex.global);
console.log("IgnoreCase flag:", regex.ignoreCase);
console.log("Multiline flag:", regex.multiline);
console.log("Unicode flag:", regex.unicode);
console.log("Sticky flag:", regex.sticky);
console.log("DotAll flag:", regex.dotAll);
Output:
All flags: gim
Global flag: true
IgnoreCase flag: true
Multiline flag: true
Unicode flag: false
Sticky flag: false
DotAll flag: false
🔹 lastIndex Property
The lastIndex property tracks position in global searches:
// Global regex remembers position
let regex = /\d+/g;
let text = "I have 5 cats and 3 dogs";
console.log("Initial lastIndex:", regex.lastIndex);
let match1 = regex.exec(text);
console.log("First match:", match1[0], "at index", match1.index);
console.log("lastIndex after first match:", regex.lastIndex);
let match2 = regex.exec(text);
console.log("Second match:", match2[0], "at index", match2.index);
console.log("lastIndex after second match:", regex.lastIndex);
let match3 = regex.exec(text);
console.log("Third match:", match3);
Output:
First match: 5 at index 7
lastIndex after first match: 8
Second match: 3 at index 18
lastIndex after second match: 19
Third match: null
🔹 Dynamic RegExp Creation
Creating RegExp objects dynamically with variables:
// Dynamic pattern creation
function createSearchRegex(searchTerm, caseSensitive = false) {
// Escape special characters
let escapedTerm = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
let flags = caseSensitive ? 'g' : 'gi';
return new RegExp(escapedTerm, flags);
}
// Test with different search terms
let searches = ["hello", "[email protected]", "price: $19.99"];
let text = "Hello world! Contact [email protected] for price: $19.99";
searches.forEach(term => {
let regex = createSearchRegex(term);
let matches = text.match(regex);
console.log(`Searching for "${term}":`, matches);
});
Output:
Searching for "[email protected]": ["[email protected]"]
Searching for "price: $19.99": ["price: $19.99"]
🔹 RegExp Object Comparison
Comparing RegExp objects and their properties:
// RegExp objects are compared by reference, not content
let regex1 = /hello/gi;
let regex2 = /hello/gi;
let regex3 = regex1;
console.log("regex1 === regex2:", regex1 === regex2);
console.log("regex1 === regex3:", regex1 === regex3);
// Compare by properties
function regexEqual(r1, r2) {
return r1.source === r2.source && r1.flags === r2.flags;
}
console.log("Content equal:", regexEqual(regex1, regex2));
console.log("regex1 source:", regex1.source);
console.log("regex2 source:", regex2.source);
console.log("regex1 flags:", regex1.flags);
console.log("regex2 flags:", regex2.flags);
Output:
regex1 === regex3: true
Content equal: true
regex1 source: hello
regex2 source: hello
regex1 flags: gi
regex2 flags: gi