简体   繁体   中英

Javascript regex trying to escape /**

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));

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. Eg parentheses are for grouping and capturing; and braces are to replicate a pattern ( a{3} = three a s). Ie do a find and replace: {\\\\{ , [\\\\[ , (\\\\( , .\\\\. .

And having newlines in the expression will probably not work either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM