How to use the xregexp.escape function in xregexp

To help you get started, we’ve selected a few xregexp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sogehige / sogeBot / src / bot / expects.js View on Github external
argument (opts) {
    opts = opts || {}
    _.defaults(opts, {
      type: String,
      optional: false,
      default: null,
      multi: false,
      delimiter: '"',
    })
    if (!opts.multi) opts.delimiter = ''
    opts.delimiter = XRegExp.escape(opts.delimiter)

    if (_.isNil(opts.name)) throw Error('Argument name must be defined')
    if (!opts.optional) this.checkText()

    let pattern
    if (opts.type === 'uuid') {
      pattern = '[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}';
    } else if (opts.type.name === 'Number') pattern = '[0-9]*'
    else if (opts.type.name === 'Boolean') pattern = 'true|false'
    else if (!opts.multi) pattern = '\\S+'
    else pattern = `(?:(?!-[a-zA-Z]).)+${opts.delimiter !== '' ? '?' : ''}` // capture until -something or [^-]*

    const fullPattern = `-${opts.name}\\s${opts.delimiter}(?<${opts.name}>${pattern})${opts.delimiter}`
    const regexp = XRegExp(fullPattern, 'ix')
    const match = XRegExp.exec(this.text, regexp)
github sogehige / sogeBot / src / bot / systems / moderation.ts View on Github external
if (permId) {
      const cLinksIncludeClips = (await this.getPermissionBasedSettingsValue('cLinksIncludeClips'))[permId];
      if (!cLinksIncludeClips) {
        clipsRegex = /.*(clips.twitch.tv\/)(\w+)/;
        text = text.replace(clipsRegex, '');
      }
    }

    text = ` ${text} `;
    const whitelist = this.cListsWhitelist;

    for (const value of whitelist.map(o => o.trim().replace(/\*/g, '[\\pL0-9\\S]*').replace(/\+/g, '[\\pL0-9\\S]+'))) {
      if (value.length > 0) {
        let regexp;
        if (value.startsWith('domain:')) {
          regexp = XRegExp(` [\\S]*${XRegExp.escape(value.replace('domain:', ''))}[\\S]* `, 'gi');
        } else { // default regexp behavior
          regexp = XRegExp(` [^\\s\\pL0-9\\w]?${value}[^\\s\\pL0-9\\w]? `, 'gi');
        }
        // we need to change 'text' to ' text ' for regexp to correctly work
        text = XRegExp.replace(` ${text} `, regexp, '').trim();
      }
    }
    return text.trim();
  }
github sogehige / sogeBot / src / bot / systems / moderation.js View on Github external
const includeClips = this.settings.links.includeClips

    if (!includeClips) {
      clipsRegex = /.*(clips.twitch.tv\/)(\w+)/
      text = text.replace(clipsRegex, '')
    }

    text = ` ${text} `
    let whitelist = this.settings.lists.whitelist

    for (let value of whitelist.map(o => o.trim().replace(/\*/g, '[\\pL0-9\\S]*').replace(/\+/g, '[\\pL0-9\\S]+'))) {
      if (value.length > 0) {
        let regexp
        if (value.startsWith('domain:')) {
          regexp = XRegExp(` [\\S]*${XRegExp.escape(value.replace('domain:', ''))}[\\S]* `, 'gi')
        } else { // default regexp behavior
          regexp = XRegExp(` [^\\s\\pL0-9\\w]?${value}[^\\s\\pL0-9\\w]? `, 'gi')
        }
        // we need to change 'text' to ' text ' for regexp to correctly work
        text = XRegExp.replace(` ${text} `, regexp, '').trim()
      }
    }
    return text
  }
github sogehige / sogeBot / src / bot / systems / cooldown.ts View on Github external
const parsed = await (new Parser().find(subcommand ? `${command} ${subcommand}` : command));
      if (parsed) {
        name = parsed.command;
      } else {
        // search in custom commands as well
        if (customCommands.enabled) {
          const foundCommands = await customCommands.find(subcommand ? `${command} ${subcommand}` : command);
          if (foundCommands.length > 0) {
            name = foundCommands[0].command.command;
          }
        }
      }

      const cooldown = await getRepository(CooldownEntity).findOne({ where: { name }});
      if (!cooldown) { // command is not on cooldown -> recheck with text only
        const replace = new RegExp(`${XRegExp.escape(name)}`, 'ig');
        opts.message = opts.message.replace(replace, '');
        if (opts.message.length > 0) {
          return this.cooldownRollback(opts);
        } else {
          return true;
        }
      }
      data = [cooldown];
    } else { // text
      let [keywords, cooldowns] = await Promise.all([
        getRepository(Keyword).find(),
        getRepository(CooldownEntity).find({ relations: ['viewers'] }),
      ]);

      keywords = _.filter(keywords, function (o) {
        return opts.message.toLowerCase().search(new RegExp('^(?!\\!)(?:^|\\s).*(' + _.escapeRegExp(o.keyword.toLowerCase()) + ')(?=\\s|$|\\?|\\!|\\.|\\,)', 'gi')) >= 0;
github sogehige / sogeBot / src / bot / systems / cooldown.js View on Github external
key = parsed.command
      } else {
        // search in custom commands as well
        if (global.systems.customCommands.isEnabled()) {
          let commands = await global.db.engine.find(global.systems.customCommands.collection.data)
          commands = _(commands).flatMap().sortBy(o => -o.command.length).value()
          const customparsed = await (new Parser().find(subcommand ? `${command} ${subcommand}` : command, commands))
          if (customparsed) {
            key = customparsed.command
          }
        }
      }

      let cooldown = await global.db.engine.findOne(this.collection.data, { key })
      if (_.isEmpty(cooldown)) { // command is not on cooldown -> recheck with text only
        const replace = new RegExp(`${XRegExp.escape(key)}`, 'ig')
        opts.message = opts.message.replace(replace, '')
        if (opts.message.length > 0) return this.check(opts)
        else return true
      }
      data = [{
        key: cooldown.key,
        miliseconds: cooldown.miliseconds,
        type: cooldown.type,
        lastTimestamp: cooldown.lastTimestamp,
        timestamp: cooldown.timestamp,
        quiet: typeof cooldown.quiet === 'undefined' ? true : cooldown.quiet,
        enabled: typeof cooldown.enabled === 'undefined' ? true : cooldown.enabled,
        moderator: typeof cooldown.moderator === 'undefined' ? true : cooldown.moderator,
        subscriber: typeof cooldown.subscriber === 'undefined' ? true : cooldown.subscriber,
        follower: typeof cooldown.follower === 'undefined' ? true : cooldown.follower,
        owner: typeof cooldown.owner === 'undefined' ? true : cooldown.owner
github sogehige / sogeBot / src / panel / views / managers / songs / songs-bannedsongs.vue View on Github external
return this.items.filter((o) => {
      const isSearchInTitle = !isNil(o.title.match(new RegExp(escape(this.search), 'ig')))
      return isSearchInTitle
    })
  }
github microsoft / DevSkim-VSCode-Plugin / server / src / devskimWorker.ts View on Github external
{
                    regexModifier = regexModifier + mod;
                }
            }
        }

        //now create a regex based on the 
        let XRegExp = require('xregexp');
        switch (regexType.toLowerCase()) 
        {
            case 'regex-word':
                return XRegExp('\\b' + pattern + '\\b', regexModifier);
            case 'string':
                return XRegExp('\\b' + XRegExp.escape(pattern) + '\\b', regexModifier);
            case 'substring':
                return XRegExp(XRegExp.escape(pattern), regexModifier);
            default:
                return XRegExp(pattern, regexModifier);
        }
    }