2018-10-12 15:37:13 -04:00
---
title: Use Regular Expressions to Test a String
---
2019-07-24 00:59:27 -07:00
# Use Regular Expressions to Test a String
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2019-07-02 07:00:18 +10:00
To begin, locate the file "tests/1_unit_tests.js" and scroll to the suite of tests for 'Strings'.
2018-10-12 15:37:13 -04:00
2019-07-02 07:00:18 +10:00
This file contains multiple suites of tests for the project, and this challenge requires you to make the tests in ``` /** 15 */` `` pass.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
2019-07-02 07:00:18 +10:00
The challenge uses function `formatPeople()` to produce a string, and then compares this to a regex. Look closely at both, and determine whether the regex will match the returned string or not.
2019-07-24 00:59:27 -07:00
### Hint 2
2019-07-02 07:00:18 +10:00
Check the error messages to determine if your understanding of the regex match was correct.
2019-07-24 00:59:27 -07:00
### Hint 3
2019-07-02 07:00:18 +10:00
The lines in the test should be changed from `assert.fail()` to either `assert.match()` or `assert.notMatch()` based on the regex from the line above.
2019-07-24 00:59:27 -07:00
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2019-07-02 07:00:18 +10:00
```js
/** 15 - #match Asserts that th actual value * * /
// matches the second argument regular expression.
test('#match , #notMatch ', function() {
2019-07-24 00:59:27 -07:00
var regex = /^#\sname\:\s[\w\s]+,\sage\:\s\d+\s?$/;
2019-07-02 07:00:18 +10:00
assert.match(formatPeople('John Doe', 35), regex);
assert.notMatch(formatPeople('Paul Smith III', 'twenty-four'), regex);
});
2019-07-24 00:59:27 -07:00
```
</details>