Reputation: 19294
I'm trying to use a javascript regex and when trying to achieve /**
, I keep getting SyntaxError: Invalid regular expression:
Does not work
\/\*\*
Works
\/\* \*
Is there a special way to escape 2 stars next to each other?
Bigger picture:
I'm using this yeoman helper to assert this:
var expected = "\n\
\/\*\*\n\
\* This is my description\n\
\*\n\
\* @param req\n\
\* @param reply\n\
\*\/\n\
exports.findEm = function (req, reply) {\n\
\n\
itemDao.findEm(req.params.id, function (err, data) {\n\
if (err) {\n\
return reply(Boom.wrap(err));\n\
}\n\
reply(data);\n\
});\n\
};\n\
";
....
helpers.assertFileContent(path.resolve(process.cwd(), 'myfile.js'), RegExp(expected));
Upvotes: 0
Views: 101
Reputation: 26022
You do pass the regex as a string. You have to escape the backslash with another backslash: "\\/\\*\\*"
.
But your regex contains many meaningful characters that you need to escape, too. E.g. parentheses are for grouping and capturing; and braces are to replicate a pattern (a{3}
= three a
s). I.e. do a find and replace: {
→ \\{
, [
→ \\[
, (
→ \\(
, .
→ \\.
.
And having newlines in the expression will probably not work either.
Upvotes: 3