How to use RegExp object in JavaScript

15 Answers

0 votes
// ==================================================
// Title: Reusable Validation Function
// ==================================================
//
// Store commonly used patterns in constants
// and reuse them through helper functions.
//

const emailPattern =
    /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

function isValidEmail(email) {
    return emailPattern.test(email);
}

console.log(
    isValidEmail("alice@example.com")
);

console.log(
    isValidEmail("not-an-email")
);


/*
run:

true
false

*/

 



answered Oct 11 by avibootz
0 votes
// ==================================================
// Title: Finding Capitalized Words
// ==================================================
//
// Find words that begin with a capital letter.
//

const text = "Albus visited Paris with Bob";

const pattern = /\b[A-Z][a-z]+\b/g;

const words = text.match(pattern);

console.log(words);


/*
run:

[ 'Albus', 'Paris', 'Bob' ]

*/

 



answered Oct 11 by avibootz

Related questions

1 answer 177 views
1 answer 193 views
1 answer 218 views
...