使用正则expression式find并获得特定的单词
我试图从数据返回中find并获得特定的词
当我执行一个单独的命令console.log(stdout)
打印下面的数据
commit e6c4236951634c67218172d742 Author:ttpn <testuser@gmail.com> Date: Mon Jun 9 10:18:04 2014 -0700 Fix it dersk reset .......... ...........
我的代码
var getemail = function (callback) { var command = "git show ec6c4236951634c67218172d742"; exec(command, function (error, stdout, stderr) { if (error !== null) { console.log(error) callback(error, null); } else { console.log(stdout) // prints the above data } })
; }
从这个我怎么能得到email id
testuser@gmail.com
使用正则expression式是可能的?
这个正则expression式将捕获你想要的组1和2的数据(在演示上 ,查看右侧窗格中的捕获组):
commit (\S+)[\r\n]*Author:[^<]*<([^>]*)>
这里是如何在JS中使用这个:
var regex = /commit (\S+)[\r\n]*Author:[^<]*<([^>]*)>/; var match = regex.exec(string); if (match != null) { theid = match[1]; theemail = match[2]; }
解释正则expression式
commit # 'commit ' ( # group and capture to \1: \S+ # non-whitespace (all but \n, \r, \t, \f, # and " ") (1 or more times (matching the # most amount possible)) ) # end of \1 [\r\n]* # any character of: '\r' (carriage return), # '\n' (newline) (0 or more times (matching # the most amount possible)) Author: # 'Author:' [^<]* # any character except: '<' (0 or more times # (matching the most amount possible)) < # '<' ( # group and capture to \2: [^>]* # any character except: '>' (0 or more # times (matching the most amount # possible)) ) # end of \2 > # '>'