Regular expressions

1. Evaluation

let re;
re = /Hello/i; // i = case insensitive
re = /hello/g; // Global search

console.log(re);
console.log(re.source);

exec() – Return result in an array or null

const result = re.exec('hello world');

console.log(result);
console.log(result[0]);
console.log(result.index);
console.log(result.input);

test() – Returns true or false

const result = re.test('Hello');
console.log(result);

match() = Return result array or null

const str = 'Hello There';
const result = str.match(re);
console.log(result);

search() – Returns index og the first match if not found returns -1

const str = 'Gabor Hello There';
const result = str.search(re);
console.log(result);

replace() – Return new string with some or all matches of a pattern

const str = 'Hello There';
const newStr = str.replace(re, 'Hi');

console.log(newStr);

2. Metacharacter Symbols

let re;

// Literal Characters
re = /hello/;
re = /hello world/i;

// Metacharacter Symbol
re = /^h/i; // Must start with h

re = /d$/i; // Must end with d
re = /worldd$/i; // Must end with d
re = /^hello$/i; // Must begin and end with
re = /^h.llo$/i; // Matches any ONE character
re = /^h*llo$/i; // Matches any character 0 or more times


re = /gre?a?y/i; // Optional character
re = /gre?a?y\?/i; // Escape character

// Brackets [] - Character Sets
re = /gr[ae]y/i; // Must be an a or e
re = /[GF]ray/i; // Must be a G or F
re = /[^GF]ray/i; // Must anything except a G or F
re = /[A-Z]ray/i; // Match any uppercase letter
re = /[a-z]ray/i; // Match any lowercase letter
re = /[A-Za-z]ray/i; // Match any letter
re = /[0-9][0-9]ray/i; // Match any digit


// Braces {} - Quantifiers
re = /Hel{2}o/i;  // Must occur exactly {m} amount of times

re = /Hel{2,4}o/i;  // Must occur at least {m} amount of times

// Parenthesis () - Grouping
re = /^([0-9]x){3}/i;

// String to march
const str = '3x3x3x';


// Log Results
const result = re.exec(str);
console.log(result);

function reTest(re, str){
    if(re.test(str)){
        console.log(`${str} matched ${re.source}`);
    } else{
        console.log(`${str} does NOT match ${re.source}`);
    }
}

reTest(re, str);
Was this page helpful?

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *