Created by
var userEmail = "prefix@domain.com";
//the RegEx to find if the userEmail contains '@' symbol:
var re = /@/;
// do the test:
if ( re.test(userEmail) ){
console.log(`Match`);
}else{
console.log(`No match!`);
}
var strings = [
'alabala',
' alabala',
'Astronomy',
'the apple'
];
var re = /^a/;
strings.forEach((str)=>
re.test(str) ?
console.log(str+' -> match!') :
console.log(str+' -> NO match!')
)
/^a/
matches each string starting with 'a'a
is a regular symbol^
is a special symbolNext characters has special meaning in Regex:
^ $ \ . * + ? ( ) [ ] { } |
They can be combined with ordinary characters to change their meaning too
If we want to match literally a special character we have to escape it with backslash '\'
They reflects how the regular expression is executed.
Modifier | Description |
---|---|
i | case-insensitive matching |
g | global match (find all matches rather than stopping after the first match) |
m | multiline matching |
var matched, str = `alAbAla`;
matched = str.match(/a/); // no flags
console.log(`matched: ${matched}`);
// matched: a (the first one)
matched = str.match(/a/g); // g flag added
console.log(`matched: ${matched}`);
//matched: a,a
matched = str.match(/a/gi); // g and i flags
console.log(`matched: ${matched}`);
//matched: a,A,A,a
[abc]
.-
), when it is between 2 symbols, has special meaning inside the character class - it defines a range. Like: [0-9]
. If it is in the end, it is considered as a hyphen.Character set | Description |
---|---|
[abc] | Match any one of the symbols listed ('a' or 'b' or 'c') |
[a-z] | Match any symbol, from 'a' till 'z' (i.e. any lower Latin letter) |
[^abc] | Match any symbol, except 'a or 'b' or 'c' (i.e. the ^ negates the characters in the set) |
// match single vocals
matched = "asteroid".match(/[aeiouy]/g);
console.log(`matched: ${matched}`);
// matched: a,e,o,i
// match any consecutive vocals
matched = "asteroid".match(/[aeiouy]+/g);
console.log(`matched: ${matched}`);
// matched: a,e,oi
// match bg mobile phone numbers
matched = "+359888123456".match(/\+3598[7-9][0-9]{7}/g);
console.log(`matched: ${matched}`);
// matched: +359888123456
Char class | Description |
---|---|
. | Match any character, except newline/line terminator. |
\w | Match word character (a character from a-z, A-Z, 0-9, including the _ (underscore) character.) |
\d | Match any Arabic digit ( from 0 to 9) |
\s | Match any whitespace character(space, tab, form feed, line ending, etc.) |
// match bg mobile phone numbers
matched = "+359888123456".match(/\+3598[7-9]\d{7}/g);
console.log(`matched: ${matched}`);
// matched: +359888123456
var re = /[a-z]\w+/;
var strings = [
'petrov42',
'42petrov',
'ivan_pterov',
]
strings.forEach(str=>console.log(`${str.match(re)} matched in ${str}:`));
// petrov42 matched in petrov42:
// petrov matched in 42petrov:
// ivan_pterov matched in ivan_pterov:
Quantifier | Description |
---|---|
r * | r match 0 or more times |
r + | r match 1 or more times |
r ? | r match 0 or 1time |
r {n} | r match exactly n times |
r {n,m} | r match between n and m times (n, m are positive) |
r
can be any regex!
var matched, str = `ala bala`;
matched = str.match(/a.*a/);
console.log(`matched: ${matched}`); //matched: ala bala
?
'
var matched, str = `ala bala`;
matched = str.match(/a.*?a/);
console.log(`matched: ${matched}`); //matched: ala
matched = "ala aa bala".match(/a.?a/g);
console.log(`matched: ${matched}`);
// matched: ala,aa,ala
matched = "ala aa bala".match(/a.{3,5}a/g);
console.log(`matched: ${matched}`);
// matched: ala aa
matched = "ala aa bala".match(/a.{3,}a/g);
console.log(`matched: ${matched}`);
// matched: ala aa bala
matched = "ala aa bala".match(/a.{3,}?a/g);
console.log(`matched: ${matched}`);
// matched: ala a,a bala
Anchor | Description |
---|---|
^ | Matches the beginning of the string (or the line, if m flag is used) |
$ | Matches the end of the string (or the line, if m flag is used) |
\b | Matches on word boundaries, i.e. between word(\w) and non-word(\W) characters. Note that the start and end of string are considered as non-word characters. |
var re = /\b/g;
var strings = [
'',
'a',
'@',
'aa',
'a!',
'a,a',
]
strings.forEach(str=>{
var res = str.match(re);
res && console.log(`${res.length} matches in '${str}'`)
});
// 2 matches in 'a'
// 2 matches in 'aa'
// 2 matches in 'a!'
// 4 matches in 'a,a'
var re = /^a\w+\a$/g;
var strings = [
'ana',
'ana bel',
]
strings.forEach(str=>{
var res = str.match(re);
res && console.log(`${res.length} matches in '${str}'`)
});
// 1 matches in 'ana'
var re = /\b[\w-]+\b/gi;
var strings = [
'one two three four, five, six. Seven!',
'one-two,three!',
];
strings.forEach(str=>{
var res = str.match(re);
res && console.log(`${res.length} matches in '${str}'`)
});
// 7 matches in 'one two three four, five, six. Seven!'
// 2 matches in 'one-two,three!'
Alternation | Description |
---|---|
r1|r2 | Matches if r1 OR r2 is matched |
// NB: this is not example of good practice for grouping regex. Why? => check next slides
var re = /\b(straw|rasp)?berries/;
var strings = [
'Icecream with strawberries? Yes!',
'Icecream with blueberries? No!',
'Icecream with raspberries? Yes!',
'Icecream with berries? Yes!',
]
strings.forEach(str=> str.match(re) ?
console.log(`${str} YES! YES!`) : console.log(`${str} NO! NO!`)
)
// Icecream with strawberries? Yes! YES! YES!
// Icecream with blueberries? No! NO! NO!
// Icecream with raspberries? Yes! YES! YES!
// Icecream with berries? Yes! YES! YES!
/(r1|r2)r3/
=> match r1r3
OR r2r3
, but not r1r2r3
/(r1)r2/
=> match r1r2
and capture the part of the string that matched r1
(?:r1|r2)
=> match r1
or r2
but do not capture the match
var re = /\b(?:straw|rasp)?berries/;
var strings = [
'Icecream with strawberries?',
'Icecream with blueberries?',
'Icecream with raspberries?',
'Icecream with strawraspberries?',
'Icecream with berries?',
];
strings.forEach(str=>str.match(re) ?
console.log(`${str} YES!`) : console.log(`${str} NO!`)
);
// Icecream with strawberries? YES!
// Icecream with blueberries? NO!
// Icecream with raspberries? YES!
// Icecream with strawraspberries? NO!
// Icecream with berries? YES!
var regexes = [
// task: match only 'strawberries' or 'raspberries':
/\bstraw|rasp{1}berries/, // not what we want
/(?:\bstraw)|(?:rasp{1}berries)/, // the same as above!!!
/\b(?:straw|rasp){1}berries/, // That's it!
];
var strings = [
'Icecream with strawberries?',
'Icecream with raspberries?',
'Icecream with straw?',
'Icecream with whateverraspberries?',
];
regexes.forEach(re=>{
console.log(`\nMatched with: ${re}`);
strings.forEach(str=>str.match(re) ?
console.log(`${str} YES!`) : console.log(`${str} NO!`)
)
});
// Matched with: /\bstraw|rasp{1}berries/
// Icecream with strawberries? YES!
// Icecream with raspberries? YES!
// Icecream with straw? YES!
// Icecream with whateverraspberries? YES!
// Matched with: /(?:\bstraw)|(?:rasp{1}berries)/
// Icecream with strawberries? YES!
// Icecream with raspberries? YES!
// Icecream with straw? YES!
// Icecream with whateverraspberries? YES!
// Matched with: /\b(?:straw|rasp){1}berries/
// Icecream with strawberries? YES!
// Icecream with raspberries? YES!
// Icecream with straw? NO!
// Icecream with whateverraspberries? NO!
Gives the possibility to match a regex only if it is followed or not by something. I.e. we can make lookahead or lookbehind!
const passwords = [
'alabala', // false
'alaba#a', // false
'alaba9a', // false
'a1aba#9', // true
'a1@', // false
];
const re = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*]).{6,}$/;
passwords.forEach(str=>{
let matched = re.test(str);
console.log(`'${str}' => ${matched}`)
});
var myNameRE = /iva/gi;
var myNameRE = new RegExp('iva','gi');
var str = 'Maria, ivan, eli, zdravka, stoyan';
// match the first word which ends on 'a':
var re1 = /\b\w+a\b/;
// match all words which end on 'a':
var re2 = /\b\w+a\b/g;
console.log( str.match(re1).toString() ); // Maria
console.log( str.match(re2).toString() ); // Maria,zdravka
var str = 'Maria, ivan, eli, zdravka, stoyan';
// match the first word which ends on 'a':
var re1 = new RegExp('\\b\\w+a\\b');
// match all words which end on 'a':
var re2 = new RegExp('\\b\\w+a\\b', 'g');
console.log( str.match(re1).toString() ); // Maria
console.log( str.match(re2).toString() ); // Maria,zdravka
As the Regex is given as string, we have to escape the backslash. I.e: \b
=> \\b
var words = ["ябълка", "ария", "ягода", "ясен"];
// Match string starting with 'я' and ending with 'а':
// RegExp literals:
var re1 = /^я.*а$/i;
words.forEach(word=>{
// re1 is compiled only once !!!:
let re1Matched = word.match(re1);
re1Matched && console.log('re1: ' + re1Matched);
});
// RegExp Constructor
var re2 = new RegExp('^я.*а$','i');
words.forEach(word=>{
// re2 is re-compiled in each iteration:
let re2Matched = word.match(re2);
re2Matched && console.log('re2: ' + re2Matched);
})
var commentsREs = [/\/\/.*/gm, /\/\*[^]+?\*\//g];
var str = `
// single line comment 1
var x = 5;
// single line comment 2
var y = 10;
/*this is multiline
comment in JS */
const pi = 3.14;
for (let i = 0; i< x; i++) console.log(i);
`;
commentsREs.forEach( re=>{
var matched = str.match(re);
matched && matched.forEach(m=>
console.log(m.toString())
);
});
String
object methodsString
object methods
str.match(regexp)
null
.g
modifier, match()
returns an Array like object with next propertiesProperty/index | Description |
---|---|
[0] | The part of the string that mathched the Regex |
[1]..[n] | The captured groups matches, if any |
index | The string index, where the match starts. Strings indexes are 0-based! |
input | The original string |
g
modifier is present, the result is an Array like object, containing all matched substrings.
var str = 'abracadabra';
var resultSimple = str.match(/r/);
console.dir(resultSimple);
var resultGlobal = str.match(/r/g);
console.dir(resultGlobal);
// Goal: remove all vocal letters in a string
// the input string:
let str = 'The quick brown fox jumps over the lazy dog';
// the regex:
const re = /[aeiouy]/gi; // global and case-insensitive
// replace all matches with empty string:
let newStr = str.replace(re, '');
// print output:
console.log(`str: ${str}`);
console.log(`newStr: ${newStr}`);
Reference: replace @mdn
// Goal: split a string by any whitespace, or ',' or '.' sequences
// the input string:
let str = `word1, word2
word3 word4. Word5`;
// the regex: match any whitespace, or ',' or '.' sequences:
const re = /[\s,.]+/;
let words = str.split(re);
// print the words array:
console.log(words);
RegExp
object methods
function guess(letter) {
// find all matches (and their indexes) of letter in wordToGuess
let matches = wordToGuess.matchAll(new RegExp(letter,"gi"));
// replace each matched position in board array with the letter
for (const match of matches) {
gameBoard.splice(match.index,1,letter);
}
console.log(gameBoard);
}
const wordToGuess = "orinoko";
// array of currently guessed letters. Each un-guessed letter is displayed as '_'
let gameBoard = wordToGuess.replace(/\w/g,'_').split('');
// Test
guess('o');
guess('m');
guess('r');
guess('n');
guess('p');
guess('k');
a1b2c3d
'a-b-c-d
'These slides are based on
customised version of
framework