views:

78

answers:

3

i have a string like following:

Assigned to ram on 2010-04-22 12:30:13.0

i need to extract the third word (ram). I tried the below regex, its not working.

/\s[a-zA-Z]*\s/

any help to fix this would be greatly appreciated.

+1  A: 

Try

a = "Assigned to ram on 2010-04-22 12:30:13.0"

re = /\w+\W+\w+\W+(\w+)/

alert(a.match(re)[1])
stereofrog
`\s` might make more sense than `\W` ?
Ipsquiggle
@Ipsquiggle, this expression picks the third word from any sentence, not just from the given one.
stereofrog
+1  A: 

Instead of using a regular expression, why not just split the string on spaces and grab the third one?

var s = "Assigned to ram on 2010-04-22 12:30:13.0";
var third = s.split(/\s+/)[2];
// third will be "ram"
Daniel Vandersluis
Thanks a lot Daniel.
Kaartz
+2  A: 

If your input is so well defined (6 words, need to get third), why not just use string methods:

'Assigned to ram on 2010-04-22 12:30:13.0'.split(' ')[2]
SilentGhost
or: `"Assigned to ram on 2010-04-22 12:30:13.0".split(" ",3).pop()`
serg