You're viewing old version number 8. - Current version
Regular Expressions with JavaScript
http://www.w3schools.com/jsref/jsref_match.asp
no \s like in perl, so : use [\s\S]* in regex
http://stackoverflow.com/questions/1068280/javascript-regex-multiline-flag-doesnt-work
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
http://www.regular-expressions.info/javascript.html
http://www.javascriptkit.com/javatutors/redev2.shtml
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
http://www.advanced-javascript-tutorial.com/RegularExpressions.cfm#.UmbpgnBwqSo
Code Example
I assume an easier method exists to do this.
Get TOC info
login as: root
root@soupmode.com's password:
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-24-virtual i686)
* Documentation: https://help.ubuntu.com/
Last login: Thu Dec 4 19:48:53 2014 from adsl-108-73-175-147.dsl.toldoh.sbcglob al.net
root@soupmode:~# cd /home/nodejs/express
root@soupmode:/home/nodejs/express# cd Grebe
root@soupmode:/home/nodejs/express/Grebe# cd js
root@soupmode:/home/nodejs/express/Grebe/js# ls -ltr
total 80
-rw-r--r-- 1 root root 456 Nov 11 16:34 errors.js
-rw-r--r-- 1 root root 482 Nov 11 16:34 search.js
-rw-r--r-- 1 root root 1576 Nov 13 20:51 relatedposts.js
-rw-r--r-- 1 root root 1562 Nov 13 20:51 showpostsource.js
-rw-r--r-- 1 root root 1590 Nov 13 20:52 tags.js
-rw-r--r-- 1 root root 1710 Nov 13 20:52 user.js
-rw-r--r-- 1 root root 2019 Nov 14 16:45 dispatch.js.backup
-rw-r--r-- 1 root root 4610 Nov 20 19:22 versions.js
-rw-r--r-- 1 root root 844 Nov 20 20:22 pageglobals.js
-rw-r--r-- 1 root root 11233 Nov 20 21:24 stream.js
-rw-r--r-- 1 root root 2307 Nov 20 21:44 dispatch.js
-rw-r--r-- 1 root root 478 Nov 20 21:46 login.js
-rw-r--r-- 1 root root 4727 Dec 4 18:45 showpost.js
-rw-r--r-- 1 root root 536 Dec 4 19:06 regex.js
-rw-r--r-- 1 root root 812 Dec 4 19:24 regex2.js
-rw-r--r-- 1 root root 680 Dec 4 20:00 regex3.js
root@soupmode:/home/nodejs/express/Grebe/js# cat regex3.js
var txt = "asdfasdf\nasdfasdf\nasdfasdf\n<!-- header:4:11:29 p.m. >\n<! header:4:10:02 p.m. >\n<! header:4:6:11 p.m. -->\nasdfasdf\nasdf";
var res = txt.match(/<!-- header:([1-6]):(.*?) -->/mg);
for (i=0; i<res.length; i++ ) {
console.log('res ' + i + ' = ' + res[i]);
var headers = [];
var regex = /<!-- header:([1-6]):(.*?) -->/m;
if ( headers = regex.exec(res[i]) ) {
for (j=0; j<headers.length; j+=3 ) {
console.log('header 0 = ' + headers[j]);
console.log('header 1 = ' + headers[j+1]);
console.log('header 2 = ' + headers[j+2]);
}
}
}
root@soupmode:/home/nodejs/express/Grebe/js# clear
root@soupmode:/home/nodejs/express/Grebe/js# cat regex3.js
var txt = "asdfasdf\nasdfasdf\nasdfasdf\n<!-- header:4:11:29 p.m. >\n<! header:4:10:02 p.m. >\n<! header:4:6:11 p.m. -->\nasdfasdf\nasdf";
var res = txt.match(/<!-- header:([1-6]):(.*?) -->/mg);
for (i=0; i<res.length; i++ ) {
console.log('res ' + i + ' = ' + res[i]);
var headers = [];
var regex = /<!-- header:([1-6]):(.*?) -->/m;
if ( headers = regex.exec(res[i]) ) {
for (j=0; j<headers.length; j+=3 ) {
console.log('header 0 = ' + headers[j]);
console.log('header 1 = ' + headers[j+1]);
console.log('header 2 = ' + headers[j+2]);
}
}
}
Output
node regex3.js
res 0 = <!-- header:4:11:29 p.m. -->
header 0 = <!-- header:4:11:29 p.m. -->
header 1 = 4
header 2 = 11:29 p.m.
res 1 = <!-- header:4:10:02 p.m. -->
header 0 = <!-- header:4:10:02 p.m. -->
header 1 = 4
header 2 = 10:02 p.m.
res 2 = <!-- header:4:6:11 p.m. -->
header 0 = <!-- header:4:6:11 p.m. -->
header 1 = 4
header 2 = 6:11 p.m.
From JR's : articles
460 words - 4072 chars
- 2 min read
created on
updated on
- #
source
- versions
Related articles
Regular Expressions with JavaScript - Dec 04, 2014
Cross-Origin Resource Sharing header - Jan 09, 2015
Web and mobile development using Perl, HTML5, CSS3, and JavaScript - May 28, 2013
Will Twitter eliminate its 140-character post limit? - Mar 16, 2015
Learning Node.JS Programming - Nov 10, 2014
more >>