paralleldrive / riteway

Simple, readable, helpful unit tests.
MIT License
1.15k stars 35 forks source link

fix: escape regex in match #303

Closed thijsgadiot closed 3 years ago

thijsgadiot commented 3 years ago

When using riteway/match, and the pattern you try to match is a string that contains regex meta characters, match does not behave as expected.

Example given;

const text = '<h1>Are there any cats?</h1>';
const pattern = 'Are there any cats?';
const contains = match(pattern);

/*
operator: deepEqual
expected: |-
  'Are there any cats?'
actual: |-
  'Are there any cats'
*/

match will try and match against the pattern Are there any cats and has omitted the ?, as it is interpreted as a regex meta character.

The code change in this PR escapes any regex meta characters when the supplied pattern is of type String. Please see the added test that documents this behaviour.