Patterns and Flags
Regular Expressions: Patterns and Flags
What is a Regular Expression (Regex) in JavaScript?
View Answer:
Here's an example of how you might use a regular expression in JavaScript to match all instances of the word "hello" in a string, regardless of case:
var myString = "Hello world! hello universe! Hello people!";
// This is our regular expression. 'g' stands for global, 'i' stands for case insensitive
var myRegex = /hello/gi;
// Use the match() function to return an array of all matches
var matches = myString.match(myRegex);
console.log(matches); // Output: ["Hello", "hello", "Hello"]
In this example, myRegex
is a regular expression that will match the string "hello" in a case-insensitive way (due to the 'i' flag). The 'g' flag means that the regular expression should match all occurrences of "hello", not just the first one. The match()
function is then used to find all matches in myString
and return them as an array.
Note: Regular expressions can be very complex and powerful, capable of much more than just simple string matching. They can also include various special characters to match specific types of character sequences, etc.
What is a flag in Regular Expressions?
View Answer:
Here is a code example that uses the 'g' and 'i' flags:
let str = "Hello, hello, HELLO";
let regex = /hello/gi; // This will match 'hello' regardless of case, and find all matches in the string, not just the first
let matches = str.match(regex);
console.log(matches); // Output: ["Hello", "hello", "HELLO"]
In this code, 'g' and 'i' flags are used in the regular expression /hello/gi
. The 'g' flag tells the script to find all matches, not just the first, and 'i' makes the search case-insensitive. Hence it matches "Hello", "hello", and "HELLO" in the string.
What are the two syntaxes used to create a regular expression?
View Answer:
// Long Syntax
regexp = new RegExp('pattern', 'flags');
// Short Syntax
regexp = /pattern/; // no flags
regexp = /pattern/gim; // with flags g,i and m (to be covered soon)
Can you name the six flags used in regular expressions?
View Answer:
Flag Descriptions:
- With the i flag the search is set to case insensitive.
- The g flag the search is set to global and looks for all matches, without it only the first search match is returned.
- The m flag represents a query in multiline mode.
- The s flag enables dot all mode allows a dot to match the newline character.
- The u enables full Unicode support, the flag enables correct processing of surrogate pairs.
- Sticky flag y sets the stick mode that allows search for the exact position of the text.
What does the 'g' flag do in a regular expressions?
View Answer:
Here's a code example that demonstrates this:
var str = "The cat sat on the mat.";
// Regular expression without 'g' flag. It will stop after the first match
var regex1 = /the/;
var result1 = str.match(regex1);
console.log(result1); // Output: ["The", index: 0, input: "The cat sat on the mat.", groups: undefined]
// Regular expression with 'g' flag. It will find all matches
var regex2 = /the/gi; // 'i' flag is for case insensitivity
var result2 = str.match(regex2);
console.log(result2); // Output: ["The", "the"]
In the first example, the regular expression /the/
finds the first instance of "the" and then stops. In the second example, the regular expression /the/gi
finds all instances of "the" in any case (due to the 'i' flag), and returns an array of all matches.
Can you explain the purpose of the 'i' flag in Regex?
View Answer:
Here's an example:
var str = "Hello World! hello world!";
// Regular expression without 'i' flag. It will match exactly 'hello'
var regex1 = /hello/g;
var result1 = str.match(regex1);
console.log(result1); // Output: ["hello"]
// Regular expression with 'i' flag. It will match 'hello' and 'Hello'
var regex2 = /hello/gi; // 'g' flag is for global search, 'i' flag is for case insensitivity
var result2 = str.match(regex2);
console.log(result2); // Output: ["Hello", "hello"]
In the first example, the regular expression /hello/g
finds only "hello" (all lowercase). In the second example, the regular expression /hello/gi
finds all instances of "hello", regardless of case, and returns an array of all matches. That's the effect of the 'i' flag: it makes the regular expression case-insensitive.
What does the 'm' flag represent in Regex?
View Answer:
Here's a code example:
var str = "Hello\nWorld";
// Regular expression without 'm' flag. ^ and $ match start and end of the string
var regex1 = /^Hello$/g;
var result1 = regex1.test(str);
console.log(result1); // Output: false
// Regular expression with 'm' flag. ^ and $ match start and end of each line
var regex2 = /^Hello$/gm; // 'g' flag is for global search, 'm' flag is for multiline matching
var result2 = regex2.test(str);
console.log(result2); // Output: true
In the first example, /^Hello$/g
tries to match the string that starts and ends with "Hello", but it doesn't find any match because the string "Hello\nWorld" starts with "Hello" and ends with "World".
In the second example, /^Hello$/gm
matches the string that starts and ends with "Hello" on any line. It finds a match because "Hello" is the entire content of the first line in the multiline string "Hello\nWorld". The 'm' flag makes ^
and $
match the start and end of each line, rather than the start and end of the whole string.
Can you combine flags in Regex? How?
View Answer:
Here's an example where 'g', 'i', and 'm' flags are combined:
var str = `Hello world
hello WORLD
HELLO WORLD`;
// Regular expression with 'g', 'i', and 'm' flags
var regex = /hello/gim; // 'g' flag is for global search, 'i' flag is for case insensitivity, 'm' flag is for multiline matching
var result = str.match(regex);
console.log(result); // Output: ["Hello", "hello", "HELLO"]
In this example, /hello/gim
is a regular expression with the 'g', 'i', and 'm' flags. The 'g' flag means that the search should find all matches, not just the first. The 'i' flag means the search should be case-insensitive. The 'm' flag means ^
and $
match start and end of each line, rather than start and end of the whole string.
As a result, the match()
function finds all instances of "hello", regardless of case and across multiple lines, and returns them as an array.
What does the 's' flag do in JavaScript regular expressions?
View Answer:
Here's a code example demonstrating the 's' flag:
var str = "Hello\nWorld";
// Regular expression without 's' flag. The dot does not match newline
var regex1 = /Hello.World/;
var result1 = regex1.test(str);
console.log(result1); // Output: false
// Regular expression with 's' flag. The dot matches newline
var regex2 = /Hello.World/s; // 's' flag is for dotAll (allowing . to match newlines)
var result2 = regex2.test(str);
console.log(result2); // Output: true
In the first example, /Hello.World/
tries to match "Hello", followed by any character, followed by "World". But it fails because the dot does not match the newline character between "Hello" and "World".
In the second example, /Hello.World/s
successfully matches "Hello\nWorld". The 's' flag makes the dot match the newline character between "Hello" and "World".
What does the u flag signify in JavaScript regex?
View Answer:
Here's an example:
// Without 'u' flag
var regex1 = /^.$/;
console.log(regex1.test('💻')); // Output: false
// With 'u' flag
var regex2 = /^.$/u;
console.log(regex2.test('💻')); // Output: true
The character '💻' is a unicode symbol that takes up two JavaScript characters (it's a "surrogate pair"), so a regular expression without the 'u' flag will not treat it as a single character. As a result, /^.$/
(a regex that matches any string with exactly one character) returns false when tested against '💻'.
However, with the 'u' flag, the regular expression /^.$/u
correctly recognizes '💻' as a single unicode character, so it matches the pattern and test()
returns true.
What's the purpose of the y flag in regular expressions?
View Answer:
Here's a code example:
var str = "Hello Hello Hello";
// Regular expression without 'y' flag. It finds the first 'Hello' it can, regardless of lastIndex
var regex1 = /Hello/g;
regex1.lastIndex = 6;
console.log(regex1.exec(str)); // Output: ["Hello", index: 6, input: "Hello Hello Hello", groups: undefined]
// Regular expression with 'y' flag. It tries to find 'Hello' exactly at position 6
var regex2 = /Hello/y;
regex2.lastIndex = 0;
console.log(regex2.exec(str)); // Output: ["Hello", index: 0, input: "Hello Hello Hello", groups: undefined]
regex2.lastIndex = 6;
console.log(regex2.exec(str)); // Output: ["Hello", index: 6, input: "Hello Hello Hello", groups: undefined]
regex2.lastIndex = 7;
console.log(regex2.exec(str)); // Output: null, because there is no 'Hello' starting at index 7
In this example, the 'y' flag causes the regular expression /Hello/y
to only find a match if it starts at the exact position indicated by regex2.lastIndex
. This allows you to control precisely where in the string the regular expression should look for matches.
How does the match method work with Regular Expressions?
View Answer:
let myString = 'We will, we will rock you';
console.log(myString.match(/we/gi)); // We,we (an array of 2 substrings that match)
Can you explain the three regex modes of the match method?
View Answer:
Here are JavaScript code examples of the three modes:
- Single match:
let str = "Hello world!";
let result = str.match(/world/); // Returns ["world"]
- Global match:
let str = "Hello world, world!";
let result = str.match(/world/g); // Returns ["world", "world"]
- Sticky match:
let str = "Hello world!";
let regex = /world/y;
regex.lastIndex = 6;
let result = str.match(regex); // Returns ["world"]
In the sticky match, the lastIndex
property sets the index at which to start the next match.
Is there a way to handle empty or null values in regular expressions?
View Answer:
// Wrong Approach
let matches = 'JavaScript'.match(/HTML/); // = null
if (!matches.length) {
// Error: Cannot read property 'length' of null
console.log('Error in the line above');
}
// Correct Approach
let matches = 'JavaScript'.match(/HTML/) || [];
if (!matches.length) {
console.log('No matches'); // now it works
}
How does the string replace method work with Regular Expressions?
View Answer:
// no flag g
console.log('We will, we will'.replace(/we/i, 'I')); // I will, we will
// with flag g
console.log('We will, we will'.replace(/we/gi, 'I')); // I will, I will
console.log('I love HTML'.replace(/HTML/, '$& and JavaScript'));
// returns - I love HTML and JavaScript
Is there a way to test for a string fragment using a regular expression?
View Answer:
let str = 'I love JavaScript';
let regexp = /LOVE/i;
console.log(regexp.test(str)); // true