Search code examples
javascriptassertchai

Match on regexp in assert throws


I have a problem with assert.throws in javascript.

assert.throws(() => someFunction(someArgument),
            /string1 (\w+) string2 string 3);

I want this expression consistent with string1 bla bla bla bla string2 string 3


Solution

  • \w won't match spaces but only letters & digits

    Try this instead.

    string1 ([\w\s]+) string2 string 3
    

    \s matches blanks (spaces, tabs, CR, LF). You can use ([\w ]+) in the line above too if you have problems with \s

    Be careful: lots of strings contain _ (underscore) not matched by the above. Use ([\w_ ]+). And if it doesn't matter what you match between the given strings, just (.+)