In this lesson, we will look at the
test
method, which checks if
a string contains at least one match
with a regex. If there is, true
is returned, and if not, false
.
The method takes a string as a parameter,
and is applied to a regex, like this:
regex.test(where to search)
For example, let's check some string for compliance with a regex:
/a+/.test('eee aaa bbb'); // returns true
Often this method is used to check if a regular expression matches an entire string. In this case, at the beginning of the regex they put a hat, and at the end - a dollar:
/^a+$/.test('aaaaaaaaa'); // returns true
Determine whether a passed string
starts with 'http://'
.
Determine whether a passed string
begins with 'http://'
or
'https://'
.
Determine whether a passed string
ends with the extension 'txt'
,
'html'
, or 'php'
.
Determine whether a passed string ends
with the extension 'jpg'
or
'jpeg'
.
Determine whether a passed string ends
with the extension 'jpg'
,
'jpeg'
, or 'png'
.
Determine whether a passed string is
'a number having from
.
1
to
12
digits'
Determine whether a passed string is a date in the year-month-day format.
Determine whether a passed string is a date in the day.month.year format.
Determine whether a passed string is a time in the hours:minutes:seconds format.
Determine whether a passed string is a valid email.
Determine whether a passed string is a domain name.