Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript regex

const text="The quick brown FOX jumps over the lazy dog. The Fox..."

const regexLiteral = /fox/i; // Word fox case insensitive
const regex = new RegExp('fox', "i" /*case insenitive flag*/);
console.log(regex.toString()) // Outputs /fox/i => The definitions are the same

// Usage
const indexOfTheFirstMatch = text.search(/fox/i) // indexOfTheFirstMatch = 16
const withLowerCaseFirstFox = text.replace(/fox/i, "fox");
// The first occurrence of fox is uncapitalized. withLowerCaseFirstFox is 
// "The quick brown fox jumps over the lazy dog. The Fox..."
const withLowerCaseFox = text.replace(/fox/gi, "fox");
// All occurrences of  word fox are uncapitalized. withLowerCaseFox is 
// "The quick brown fox jumps over the lazy dog. The fox..."
// text.replaceAll(/fox/i, "fox") is equivalent to text.replace(/fox/gi, "fox")

const firstFoxText = text.match(/fox/g); // firstFoxText = ["FOX"] 
const foxOccurences = text.match(/fox/gi) // foxOccurences = ["FOX","Fox"]

// Check also regex methods: regex.match(), search(), replace(), etc.
Source by eloquentjavascript.net #
 
PREVIOUS NEXT
Tagged: #javascript #regex
ADD COMMENT
Topic
Name
8+8 =