Explorar o código

implemented the RegExp so that it will escape special Character

白石誠 %!s(int64=5) %!d(string=hai) anos
pai
achega
773b6bb29f
Modificáronse 1 ficheiros con 2 adicións e 1 borrados
  1. 2 1
      src/server/routes/tag.js

+ 2 - 1
src/server/routes/tag.js

@@ -82,7 +82,8 @@ module.exports = function(crowi, app) {
    * @apiParam {String} q keyword
    */
   api.search = async function(req, res) {
-    let tags = await Tag.find({ name: new RegExp(`^${req.query.q}`) }).select('-_id name');
+    const escapeRegExp = req.query.q.replace(/[\\^$/.*+?()[\]{}|]/g, '\\$&');
+    let tags = await Tag.find({ name: new RegExp(`^${escapeRegExp}`) }).select('-_id name');
     tags = tags.map((tag) => { return tag.name });
     return res.json(ApiResponse.success({ tags }));
   };