JS Regular Expressions Exercises 1

JS Regular Expressions Exercises 1

By Ifeanyi Omeata


Task 1 -

const greeting = "Hello World";

Using Regular expressions (Regex), find out if the string "Hello" is found in greeting.


/*
Using Regular expressions (Regex), find out if the string "Hello" is found in greeting.
*/
const greeting = "Hello World";

const isTrue = /Hello/.test(greeting);
console.log(isTrue);

image.png


Task 2 -

const greeting = "Hello, is that a cat?";

Using Regex, find out if any of the following: (dog,cat,mice,bird) is in the string greeting.


/*
Using Regex, find out if any of the following: (dog,cat,mice,bird) is in the string greeting.
*/
const greeting = "Hello, is that a cat?";

const isTrue = /dog|cat|mice|bird/.test(greeting);
console.log(isTrue);

image.png


Task 3 -

const greeting = "Hello, is that a cat?";

Using Regex, ensure that "hello" returns true when searched for in the string greeting.


/*
Using Regex, ensure that "hello" returns true when searched for in the string greeting.
*/
const greeting = "Hello, is that a cat?";

isTrue = /hello/i.test(greeting)
console.log(isTrue)

image.png


Task 4 -

const students = "Andrew, Mary, Shade, Bob, Peter, Mike";
const bestStudent = "Peter";

Using Regex, find out if bestStudent is found in students and log out the name of the student.


/*
Using Regex, find out if bestStudent is found in students and log out the name of the student.
*/
const students = "Andrew, Mary, Shade, Bob, Peter, Mike";
const bestStudent = /Peter/;

const student = students.match(bestStudent);
console.log(student);
console.log(student[0]);

image.png


Task 5 -

const students = "Andrew, Mary, Shade, Andrew, Peter, Andrew";
const bestStudent = "Andrew";

Using Regex, log out all the values of occurences of bestStudent in the students string.


/*
Using Regex, log out all the values of occurences of bestStudent in the students string.
*/
const students = "Andrew, Mary, Shade, Andrew, Peter, Andrew";
const bestStudent = /Andrew/g;
result = students.match(bestStudent);
console.log(result)

image.png


Task 6 -

const poem = "Twinkle, twinkle, twinKLE, little star";
let word = "twinkle";

Using Regex, log out all the values of occurences (not Case-Sensitive) of word in the poem string.


/*
Using Regex, log out all the values of occurences (not Case-Sensitive) of word in the poem string.
*/
const poem = "Twinkle, twinkle, twinKLE, little star";
let word = /twinkle/ig;
let result = poem.match(word); 

console.log(result);

image.png


Task 7 -

const myString = "ado, edu, obu, edi, edu ";

Using Regex, log out all occurances of strings in myString that starts with ed-.


/*
Using Regex, log out all occurances of strings in myString that starts with ed-.
*/
const myString = "ado, edu, obu, edi, edu ";
let word = /ed./g;
let result = myString.match(word);
console.log(result);

image.png


Task 8 -

const myString = "big, buy, bug, but, bag";

Using Regex, log out all occurances of strings (not case sensitive) in myString that has this syntax:
b-(aeiou)-g
First character: b
Second character: Any of these ('a' or 'e' or 'i' or 'o' or 'u')
Third character: g


// Task 8 -
// const myString = "big, buy, bug, but, bag";
// Using Regex, log out all occurances of strings (not case sensitive) in myString that has this syntax:
// b-(aeiou)-g
// First character: b
// Second character: Any of these ('a' or 'e' or 'i' or 'o' or 'u')
// Third character: g

const myString = "big, buy, bug, but, bag";

const str = /b[aeiou]g/ig;
const result = myString.match(str)

console.log(result)

image.png

#End


Hope you enjoyed this! :) Follow me for more contents...


Get in Touch:
ifeanyiomeata.com

Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2021**