开发者

javascript regex to extract a word from middle of 6 sentence

开发者 https://www.devze.com 2022-12-27 18:50 出处:网络
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.

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/

an开发者_如何转开发y help to fix this would be greatly appreciated.


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]


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"


Try

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

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

alert(a.match(re)[1])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号