Published
Edited
Sep 16, 2021
Insert cell
Insert cell
/katia/.test("Hello katia!"); //the regular expression here is katia, the string "Hello katia!" is tested if it contains katia
Insert cell
/katia/.test("Hello Katia!");
Insert cell
/katia/i.test("Hello Katia!"); // i flag outside / closing the regex is used to ignore case (caps or not)
Insert cell
Insert cell
/^(?!.*bruno).+$/.test("bruno");
Insert cell
/\b(?!\d+[50]0)\d+\b/.test("100");
Insert cell
/^[+-]?\d*\.?\d+$/.test("2..3")
Insert cell
/^#([a-f\d]{3}){1,2}$/i.test("#000")
Insert cell
/a(b|c)/.test("ac")
Insert cell
Insert cell
/^a/.test('abcd');
Insert cell
/a$/.test('Fahima');
Insert cell
/no$/.test('Bruno');
Insert cell
/sna$/.test('Husna');
Insert cell
/^Jon/.test('Jonathan');
Insert cell
Insert cell
/c.+t/.test('cymbalist');
Insert cell
/z*/.test('');
Insert cell
/1+2*3+/.test('1111223');
Insert cell
/1+2*3+/.test('11113');
Insert cell
/1+2+3+/.test('1111223');
Insert cell
/./.test('\n');
Insert cell

/.+/.test('');
Insert cell
/horse+/.test('horseeeeee');
Insert cell
Insert cell
/dog|cat/.test("It's raining cats and dogs.");
Insert cell
/dog|cat/.test("It's raining only cats.");
Insert cell
/dog|cat/.test("It's raining only dogs.");
Insert cell
/dog|cat/.test("It's raining men.");
Insert cell
/^a|b$/.test('ax'); // the regex here reads starts with an a or ends with a b
Insert cell
/^a|b$/.test('ghkjkjb'); // the regex here reads starts with an a or ends with a b
Insert cell
/(^a)|(b$)/.test('ax'); //brackets work like in normal JS
Insert cell
/^(a|b)$/.test('a');
Insert cell
Insert cell
/[a-zA-Z]+ [a-zA-Z]+!/.test("Hello Melissa!");
Insert cell
/^Levi/.test("Levi");
Insert cell
/cats|dogs/.test("It's raining cats and dogs.");
Insert cell
/^[a-d]/.test('abcd');
Insert cell
/emergency/.test('emergency');
Insert cell
/^a few words$/.test('a few words');
Insert cell
/.* horses$/.test('several horses');
Insert cell
/^/.test('abcd');
Insert cell
/\s/.test('\n');
Insert cell
/ct/.test('ct');
Insert cell
/1+2+/.test('11111222');
Insert cell
Insert cell
/\+/.test('+');
Insert cell
/\++/.test('++'); // \+ evaluates as a plus sign but the second plus is one or more
Insert cell
/.\+./.test('1+1');
Insert cell
/.\./.test('1+1.');
Insert cell
/\^/.test('^');
Insert cell
/\$$/.test('a$');
Insert cell
Insert cell
/\s/.test(' ');
Insert cell
/\s/.test('\n');
Insert cell
/\s/.test('');
Insert cell
/\d/.test('0');
Insert cell
/\d/.test('123');
Insert cell
/\d/.test('a');
Insert cell
/\D/.test('a');
Insert cell
/^\d*$/.test('3000');
Insert cell
/[a-c]/.test('cat');
Insert cell
/^[a-c]+$/.test('cat');
Insert cell
/^[act]+$/.test('cat');
Insert cell
/^[acth]+$/.test('cat');
Insert cell
/^[acth]+$/.test('catz');
Insert cell
/[eaouiy]/.test('abracadabra'); // tests if the string contains vowels
Insert cell
Insert cell
/^\d{3}-\d{4}$/.test("333-3333")
Insert cell
/^[\w.+-]+@[\w-]+\.[\w-.]+$/.test("bruno.silva@ada.ac.uk")
Insert cell
## Replace method in JS

You can search and replace text in a string using .replace() on a string.

Unlike .test(), .replace() is chained to a string value.

The parameters for .replace() are:

1. the regex pattern you want to search for.
2. the string to replace the match or a **function** to do something.
Insert cell
"recognize".replace(/z/g, "s"); // the g flag finds all matches in the string
Insert cell
"recognizing initialization".replace(/z/g, "s");
Insert cell
"recognizing initialization".replace(/z/, "s"); // without the g flag only the first match is replaced
Insert cell
"this code removes vowels from a string".replace(/[aeouiy]/g, '');
Insert cell
"this code removes vowels and double spaces from a string".replace(/[aeouiy]/g, '').replace(" ", " ");
Insert cell
"this code replaces vowels with stars".replace(/[aeouiy]/g, char => '⭐️');
Insert cell
"this code Makes vOweLs lowEr casE".replace(/[aeouiy]/ig, char => char.toLowerCase());
Insert cell
## .trim()

Sometimes whitespace characters around strings are not wanted. Typical processing of strings is to remove the whitespace at the start and at the end.

String.prototype.trim() method allows to do it easily.
Insert cell
" hello ".trim();
Insert cell
Insert cell
" bruno is tired ".trim();
Insert cell
"I love codewards".replace(/code/g, "s"); // swards - the upper layer of soil, especially when covered with grass.
Insert cell
"I naughtyword hate Mondays".replace(/naughtyword/g, '******')
Insert cell
"I'm happy this morning :3".replace(/:3/g, char => '😊');
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more