I don't understand why my nodejs/javascript regex isn't working -
i have simple text i'm trying parse:
total 4.0k -rw-rw-r-- 1 346 mar 1 08:50 save_1 -rw-rw-r-- 1 0 feb 28 17:28 save_2 -rw-rw-r-- 1 0 feb 28 17:28 save_3 and have regular expression have tested working on different regex testing websites:
\w{3}\s+\d{1,2}\s\d{2}\:\d{2}\s\w{4}\_\d i'm trying take sample text input in following function in node.js application , return object or array 3 different matches, month end of line.
function parse(str) { var regex = new regexp("\w{3}\s+\d{1,2}\s\d{2}\:\d{2}\s\w{4}\_\d"); return regex.test(str); //return str.match(regex); } i don't understand why boolean .test() false , object .match() null.
any appreciated.
instead of trying parse output of ls, which bad, should use file system operation provided node.js. using file system operations can sure program work in (almost) edge case output defined. work in case folder contain more or less files 3 in future!
as stated in comments want names , date / time of files folder. let have at:
fs.readdir(path, callback): fs.readdir give array of filenames in folder specified in path. can pass them fs.stat find out mtime:
fs.stat(path, callback): fs.stat() give object of fs.stats contains mtime in mtime property.
so code afterwards:
fs.readdir('dir', function (err, files) { (var = 0; < files.length; i++) { (function () { var filename = files[i] fs.stat('dir/' + filename, function (err, stats) { console.log(filename + " last changed on " + stats.mtime); }); })(); } }); the output is:
[timwolla@~/test]node test.js 5 last changed on fri mar 01 2013 20:24:35 gmt+0100 (cet) 4 last changed on fri mar 01 2013 20:24:34 gmt+0100 (cet) 2 last changed on fri mar 01 2013 20:24:33 gmt+0100 (cet) in case need return value use respective sync-versions of these methods. these, however, block node.js eventing loop.
Comments
Post a Comment