2

This regexp is matching strings after /ab. Also, i would like to match empty strings after /ab. How can i do that?. Thanks in advance.

const TelegramBot = require('node-telegram-bot-api');
const token = process.env.TOKEN;
const bot = new TelegramBot(token, { polling: true });

bot.onText(/\/ab (.+)/, (msg, match) => {
    const chatId = msg.chat.id
    const prompt = match[1]
    bot.sendMessage(chatId, prompt)
})
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35

1 Answers1

1

Use a lookbehind: /^(?![\s\S])|(?<=\/ab).*/g

A lookbehind (?<=) will match anything in front of it without being included in the actual match. As for return an empty string, you'll need to explicitly return a "" if there's no match. You can change this line maybe: Added an alternate ^(?!\[\s\S\])|

const rgx = new RegExp(/^(?![\s\S])|(?<=\/ab).*/, 'g');

const str = `/ab 1234 hdsso
/abksdfsioiowe
/ab
khbigigi7 
s366s7d /ab`;

let matches = [...str.matchAll(rgx)].flat();

console.log(matches);

Regex101

zer00ne
  • 41,936
  • 6
  • 41
  • 68