Regex Sets / Ranges
Regular Expressions: Regex Sets / Ranges
What is a set in JavaScript Regex?
View Answer:
let str = "The quick brown fox jumps over the lazy dog.";
// Define a regex with a set
let regex = /[aeiou]/gi;
// Using the regex to find all vowels in the string
let result = str.match(regex);
console.log(result); // Outputs: ['e', 'u', 'i', 'o', 'o', 'u', 'o', 'e', 'e', 'a', 'o']
Can a set include a range in Regex?
View Answer:
let str = "Hello, JS! 123";
// Define a regex with a set that includes a range
let regex = /[a-z0-9]/gi;
// Using the regex to find all alphanumeric characters in the string
let result = str.match(regex);
console.log(result); // ["H", "e", "l", "l", "o", "J", "S", "1", "2", "3"]
How do you specify multiple ranges in a Regex set?
View Answer:
let str = "Hello, JS! 123";
// Define a regex with a set that includes multiple ranges
let regex = /[a-zA-Z]/g;
// Using the regex to find all alphanumeric characters in the string
let result = str.match(regex);
console.log(result);
// Outputs: ["H", "e", "l", "l", "o", "J", "S"]
What does a caret (^) inside a Regex set indicate?
View Answer:
let str = "Hello, JS! 123";
// Define a regex with a set that includes a caret (^) to negate the set
let regex = /[^a-zA-Z0-9]/g;
// Using the regex to find all non-alphanumeric characters in the string
let result = str.match(regex);
console.log(result);
// Outputs: [',', ' ', '!', ' ', ' ']
Can Regex sets contain special characters?
View Answer:
How would you specify a regex set to match only lowercase vowels?
View Answer:
let str = "Hello my name is larry and I want you to know JavaScript!";
let regex = /[aeiou]/g;
let result = str.match(regex);
console.log(result);
// ["e", "o", "a", "e", "i", "a", "a", "a", "o", "u", "o", "o", "a", "a", "i"]
How do square brackets […] perform in regular expression sets?
View Answer:
// find [t or m], and then "op"
console.log('Mop top'.match(/[tm]op/gi)); // "Mop", "top"
// Return null or no matches
// find "V", then [o or i], then "la"
console.log('Voila'.match(/V[oi]la/)); // null, no matches
Can you explain how range is set in a regular expression?
View Answer:
console.log('Exception 0xAF'.match(/x[0-9A-F][0-9A-F]/g)); // xAF
Is there a way to handle Han (Chinese) or Cyrillic in regexp ranges?
View Answer:
Here's a code example of using Unicode ranges in JavaScript regex:
let hanRegex = /[\u4E00-\u9FFF]+/u;
console.log(hanRegex.test('汉字')); // true
let cyrillicRegex = /[\u0400-\u04FF]+/u;
console.log(cyrillicRegex.test('русский')); // true
In this example, the test()
function will return true
if the string contains at least one Han or Cyrillic character.
Unicode Property Escapes Example:
In JavaScript, Unicode property escapes \p{...}
can also be used to match characters that have certain general categories, scripts, or other properties. Here is how you might match Han (Chinese) or Cyrillic characters using properties:
let hanRegex = /\p{Script=Han}+/u;
console.log(hanRegex.test('汉字')); // true
let cyrillicRegex = /\p{Script=Cyrillic}+/u;
console.log(cyrillicRegex.test('русский')); // true
In these examples, \p{Script=Han}
matches any character in the Han (Chinese) script, and \p{Script=Cyrillic}
matches any character in the Cyrillic script. Note that the u
flag is necessary to enable this feature.
How do you exclude a range of characters in regular expressions?
View Answer:
console.log('alice15@gmail.com'.match(/[^\d\sA-Z]/gi)); // returns @ and .
Do we have to escape special characters in regex sets or ranges?
View Answer:
// No need to escape
let regexp = /[-().^+]/g;
console.log('1 + 2 - 3'.match(regexp)); // Matches +, -
// Escaped everything
let regexp = /[\-\(\)\.\^\+]/g;
console.log('1 + 2 - 3'.match(regexp)); // also works: +, -
What is the recommended way to match against surrogate pairs in a set or range?
View Answer:
Certainly! Here's a code example demonstrating how to use Unicode property escapes to match surrogate pairs, specifically for emojis, in JavaScript regex:
let emojiRegex = /\p{Emoji}/u;
console.log(emojiRegex.test('❤️')); // true
let nonEmojiRegex = /[^\p{Emoji}]/u;
console.log(nonEmojiRegex.test('Hello!')); // true
In this example, the test()
function is used to check if a string contains any emojis using the \p{Emoji}
property escape. The /u
flag is necessary to enable full Unicode matching.