0

I would like to parse some plain text that I got from the Twitter API. What I need to do is basically search through a tweet to find a "@" character followed by some text and replace the plain text with an anchor linked to that text. For example:

Take the tweet plain text: "@flintzke this is a test"

and use a Regular expression to turn it into:

<a href='www.twitter.com/flintzke'>@flintzke</a> this is a test

my guess was to use this kind of function:

function getUsernameLink(text) {
var exp = /^[@][a-zA-Z0-9_!@#%&*]*[\s]$/ig;
return text.replace(exp, "<a href='https://twitter.com/$1'>$1</a>");
}

my objective was to find an expression that found a single word in a string, text, that starts with "@" and ends with a whitespace

2
  • 1
    just edited to show my current try Nov 30, 2012 at 0:35
  • do you want the rest of the tweet placed into your result string?
    – hyleaus
    Nov 30, 2012 at 0:37

2 Answers 2

2

I think you have tried a bit to hard ;). Firstly the anchors ^ and $ mark the beginning and end of the string (not the beginning and end of the match or something - which I suppose you assumed). Then you use $1 but that corresponds to the first capture (the first set of parentheses - which you don't have). Either add parentheses around your match or use $&. And lastly, \S represents a non-whitespace character. So why not just match as many non-whitespace characters as possible?

exp = /@(\S*)/g;
return text.replace(exp, "<a href='https://twitter.com/$1'>@$1</a>");
4
  • ohh yes you are right I thought ^ and $ meant beginning of the matched string not the entire string. This looks much better! Is it possible to LOOK for the @ at the beginning of the word but not actually pull it into the $1? thanks alot Nov 30, 2012 at 0:42
  • @MattHintzke sure. Make use of capturing ;). You are free to place the parentheses wherever you want. See my edit. Nov 30, 2012 at 0:43
  • ohhh so the parenthesis is what indicates the actual "Capture" of the string? that is great! thanks Nov 30, 2012 at 0:44
  • @MattHintzke yes, that's what I meant. Sorry if that was unclear. Here is some further reading on the matter. Nov 30, 2012 at 0:49
1

Try this one :

text.replace(/\@(\w+)/g,"<a href='https://twitter.com/$1'>@$1</a>");
12
  • Not only that this will not work if the last word in the string is preceded by @, it will also remove the space between the linked word and the one after that. Nov 30, 2012 at 1:02
  • Yup. Now only the first one still applies ;). It will still not work for a @name at the end of the string. Nov 30, 2012 at 23:16
  • The below would satisfy your 1 st point but what @MattHintzke asked for was not to match a pattern at the end of the string. text.replace(/\@(\w+)/g,"<a href='https://twitter.com/$1'>@$1</a> "); Nov 30, 2012 at 23:28
  • His request was "What I need to do is basically search through a tweet to find a "@" character followed by some text and replace the plain text with an anchor linked to that text." He doesn't make any assumption about the position. I believe a solution should try to capture the OP's intent and not only work for the few specific examples he gives. Nov 30, 2012 at 23:30
  • Then my last comment should satisfy his needs except that it will add a space at the end, if the pattern found at the end of the string. Thanks Nov 30, 2012 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.