7

I need javascript regex that will match words that are NOT followed by space character and has @ before, like this:

@bug - finds "@bug", because no space afer it

@bug and me - finds nothing because there is space after "@bug"

@bug and @another - finds "@another" only

@bug and @another and something - finds nothing because both words are followed by space.

Help? Added: string is fetched from and FF puts it's own tags at end of it. Although I basically need only the last word starting with @, $ (end-of-string) can not be used.

Zeljko
  • 5,048
  • 5
  • 36
  • 46
  • 1
    When you say *"not followed by space character"*, are you really saying *"is at the end of the string"*? They're not necessarily the same thing. –  Dec 21 '11 at 04:23
  • No, it is from contenteditable div and FF puts it's own tags at the end. – Zeljko Dec 21 '11 at 05:02

2 Answers2

16

Try re = /@\w+\b(?! )/. This looks for a word (making sure it captures the whole word) and uses a negative lookahead to make sure the word is not followed by a space.

Using the setup above:

var re = /@\w+\b(?! )/, // etc etc

for ( var i=0; i<cases.length; i++ ) {
    print( re2.exec(cases[i]) )
}

//prints
@bug
null
@another
null

The only way this won't work is if your word ends in an underscore and you wanted that punctuation to be part of the word: For example '@bug and @another_ blahblah' will pick out @another since @another wasn't followed by a space. This doesn't seem very likely but if you wanted to deal with that case too, you could use /@\w+\b(?![\w ]/ and that would return null for @bug and @another_ and @bug_ for @another and @bug_.

mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
5

It sounds like you're really just looking for words at the end of the input:

/@\w+$/

Tests:

var re = /@\w+$/,
    cases = ['@bug',
             '@bug and me',
             '@bug and @another',
             '@bug and @another and something'];

for (var i=0; i<cases.length; i++)
{
    console.log(cases[i], ':', re.test(cases[i]), re.exec(cases[i]));
}

// prints
@bug : true ["@bug"]
@bug and me : false null
@bug and @another : true ["@another"]
@bug and @another and something : false null
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    The problem is that this string is from
    and for some reason, FF always adds some tag like:
    In that case, regex doesn't work
    – Zeljko Dec 21 '11 at 04:51
  • @Zeljko the example input showed nothing like this, and your question mentioned nothing about HTML. Want a better answer? Ask a better question. – Matt Ball Dec 21 '11 at 04:56
  • You are right, I didn't notice that additional tag before. Any hint on this? I tried negative lookahead and who-knows-what, but it keeps failing. – Zeljko Dec 21 '11 at 04:58
  • @Zeljko like I said, I really have no idea what the input actually looks like, so I can't really be of much help without seeing the real input you're working with. Why not just strip `
    ` before passing to the regex? Anyway - it's sleep time for me, I'll check back in the morning.
    – Matt Ball Dec 21 '11 at 05:00
  • Imagine input like:** #bug and #another** I can't just strip that tag because other browsers might put something else on their own. – Zeljko Dec 21 '11 at 05:05
  • @Zeljko one last suggestion for the night: http://stackoverflow.com/questions/3455931/extracting-text-from-a-contenteditable-div – Matt Ball Dec 21 '11 at 05:08