0

I'm completely useless with Regular expressions and need help with this one. I'm currently creating a HipChat bot for my work which will create JIRA tickets from HipChat.. HipChat bots have the ability to monitor a chat room for keywords.. then run JavaScript if the keyword has been used.

In my case, I would like the Bot to monitor for -

"/ask ********************************"

Where * = text of the JIRA issue body of unlimited length

so for this, i would need regex to hook on to, and also regex to move the description text into a variable.. would anyone here be able to assist??

If I haven't explained myself well, below is an example of how "Karma" works (addon.webhook). Thanks!

https://bitbucket.org/atlassianlabs/ac-koa-hipchat-karma/src/cca57e089a2f630d924cd5df23211f0da3617063/web.js?at=master

brandonscript
  • 68,675
  • 32
  • 163
  • 220
osmo
  • 87
  • 3
  • 10

2 Answers2

1

You could simply use this regex:

^\/ask (.*)$

It will capture the text after /ask. Note the escaped backslash here as well. Since you're using Javascript, the actual expression is used with delimiters, e.g.:

/^\/ask (.*)$/

Here's a regex101 to play with: https://regex101.com/r/tZ0iB6/1

brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • You should add `^` to require that `/ask` come at the beginning of the string, otherwise it'll match on a string with `/ask` anywhere in the middle. – ajp15243 Feb 17 '15 at 20:11
  • @ajp15243 yup! Good call – brandonscript Feb 17 '15 at 20:15
  • thank, but it fails to compile with this /^\/ask(.*)$ – osmo Feb 17 '15 at 20:17
  • @user3071089 How does it fail? We cannot help you unless you say *how* and *why* with as much info as possible, rather than just *what* happened. – ajp15243 Feb 17 '15 at 20:18
  • sorry. i hit enter for new line but it posted my reply.. this is my line which fails compile - addon.webhook('room_message', \/^ask (.*)$, function *() { ^ SyntaxError: Unexpected token ILLEGAL – osmo Feb 17 '15 at 20:19
  • @user3071089 Looks like you forgot the `/` after the `$` that closes the regex literal. – ajp15243 Feb 17 '15 at 20:21
  • thanks.. but still no go--- addon.webhook('room_message', \/^ask (.*)$/, function *() { ^ SyntaxError: Unexpected token ILLEGAL – osmo Feb 17 '15 at 20:23
  • @user3071089 Hah I looked too quickly, you also forgot the opening `/`! It needs to be exactly `/^\/ask (.*)$/`. What remus put at the top of his answer is what goes *inside* the regex literal (which is surrounded by `/` characters). – ajp15243 Feb 17 '15 at 20:25
  • @user3071089 I edited the answer to have `/` surrounding it at the top. Also I moved the `^`, as it had been accidentally put after the escaped slash (`\/`), when it should have gone before. – ajp15243 Feb 17 '15 at 20:30
  • thank you @user3071089. it now compiles. but it doesnt seem to want to match.. addon.webhook('room_message', /^\/ask (.*)$/, function *() { /ask test /asktest /ask – osmo Feb 17 '15 at 20:49
  • @user3071089 are you certain that addon.webhook supports regular expressions like this? See this jsfiddle: it does match. http://jsfiddle.net/ff9f8js0/ – brandonscript Feb 17 '15 at 20:56
  • @user3071089 hmmm im not sure! can you see the difference in the example i posted (karma) ? – osmo Feb 17 '15 at 21:07
  • @user3071089 it would help if you edited your question and posted the function there with code formatting – brandonscript Feb 17 '15 at 21:12
0

It's a quiet simple regex:

var matches = /^\/ask\s+(.*)$/i.exec(str);
if (matches && matches.length > 0) {
    var content = matches[1];
}

If you want to match multiple different command you can use something like this:

var matches = /^\/([^\s]+)\s+(.*)$/g.exec(str);
if (matches && matches.length > 0) {
    var command = matches[1];
    var content = matches[2];
    switch(command.toLowerCase()) {
        case 'ask':
            // do something
            break;
        default:
            // command not found
            break;
    }
}

Edit:

  1. Fixed the 'a' variable
  2. the variable str has to be the input string.

    var str = '/ask *****';

Simon
  • 240
  • 1
  • 6
  • I edited the post above. replaced the var a through str. It has to be your input string. – Simon Feb 17 '15 at 20:07
  • Don't do this! Wrap your code inside a `(function(str,undefined){ [code] })(str);`! – Ismael Miguel Feb 17 '15 at 20:08
  • thank you.. but i need the regex for the "hook" to enter my code in the first place.. this doesn't work with your formula – osmo Feb 17 '15 at 20:16
  • You may try this one. I can't test it myself: addon.webhook('room_message', \/^ask\s+.+/, function *() { var str = this.content; [...] }); replace the [...] with the snippet in the answer. – Simon Feb 17 '15 at 20:37
  • @simon thank you. but addon.webhook('room_message', \/^ask\s+.+/, function *() { doesnt compile :/ – osmo Feb 17 '15 at 20:44