How to evaluate a string on a regular expression rule in JavaScript

JavaScript has a built in regular expression function for testing if a string arguments is matching the given regular expression rule;

Syntax of the regular expression tester is as follows;

/regex/.test(string)

Take for instance you want to test if a string contains only alphanumeric upper and lower case characters, space and numbers;

const str = "Eat code sleep"

/[A-Z,a-z,0-9, ]*/.test(str)
// returns boolean 'true'

Or in another example;

const str = "Eat, Code, Sleep"

/[A-Z,a-z,0-9, ]*/.test(str)
// returns boolean 'false'

Because commas are not included in the regular expression match rules.

Comments

Popular posts from this blog

JavaScript introduction to variables

Solutions to blocked URLs on Facebook

How to set content disposition headers for express nodejs apps

How to stop moz dotbot from accessing your website

how to dump a postgresql database using from the terminal