Question Bank

JavaScript String-Only Regex Match: Three Approaches Quiz

Difficulty: Medium

Validate that a string contains only letters and whitespace, three ways (`String.prototype.match` + literal, `RegExp.test`, including-special-chars via `\D`), plus companions on Unicode letters and inverting the rule.

Question Bank
/

JavaScript String-Only Regex Match: Three Approaches Quiz

JavaScript String-Only Regex Match: Three Approaches Quiz

Validate that a string contains only letters and whitespace, three ways (`String.prototype.match` + literal, `RegExp.test`, including-special-chars via `\D`), plus companions on Unicode letters and inverting the rule.

Question Bank
Medium
JavaScript
5 questions
quiz
regex
strings
js-language

510 views

4

Implement matchString(str) that returns true when str contains ONLY ASCII letters (a-z, A-Z) and whitespace; otherwise return false. Use String.prototype.match with the literal regex /^[A-Za-z\s]+$/.

Examples

Example 1:

Input: 'abc def'
Output: true
Explanation: Letters and a single space match the character class.

Example 2:

Input: 'abc 123'
Output: false
Explanation: Digits are outside [A-Za-z\s].