Procházet zdrojové kódy

implemented the RegExp so that it will escape special Character

白石誠 před 5 roky
rodič
revize
773b6bb29f
1 změnil soubory, kde provedl 2 přidání a 1 odebrání
  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 }));
   };