I’m trying to create a small app that reads a chat message and echoes a filtered text
I need it to take this message:
INPUT:
#notthis/remove
remove this line
This one too
Output:
notthis
At the moment all it does is remove the second word + the first hashtag:
"hello#hello"
which becomes hello
I tried adding the /
like this input.split("#","/");
but all it does is crash the program.
Not asking to do the program for me, but I’d really appreciate any hints.
Thank you!
const Telegraf = require('telegraf');
const bot = new Telegraf('182049');
const helpMessage = `
Say something to me
/start - start the bot
/help - command reference
`;
bot.start((ctx) => {
ctx.reply("Hi I am echoo bot");
ctx.reply(helpMessage);
});
bot.help((ctx) => {
ctx.reply(helpMessage)
});
bot.on("text", (ctx) => {
let input = ctx.message.text;
let inputArray = input.split("#");
console.log(inputArray);
let message = "";
if (inputArray.length == 1) {
message = "no separator";
} else {
inputArray.shift();
message = inputArray.join(" ");
}
ctx.reply(message);
});
bot.launch()
2
Answers
I think I made it
Only a little issue: if my coin has more than 3 letters it gets truncated. For loop solves the problem but every letter starts with a new line, and the second imput gets messed up
2nd attempt:
Regular Expression (regex) is a great way to find substrings in an unknown string, even though it might be a bit daunting to work with. You can use this website to create your regex.
To find a substring starting with
#
and ending with/
, you can use the following expression:This will match 1 or more word characters that are in between a
#
and a/
.For example:
#foo/
will match#f/
will match#/
will not match#foo bar/
will not match (whitespace is not a word character)An example JavaScript code: