1 min

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FRegular_Expressions

http://www.advanced-javascript-tutorial.com/RegularExpressions.cfm#.UmbpgnBwqSo

Code Example

I assume an easier method exists to do this.

Get TOC info


    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.

#regex - #javascript - #programming

From JR's : articles
189 words - 1804 chars - 1 min read
created on
updated on - #
source - versions

Related articles
Will Twitter eliminate its 140-character post limit? - Mar 16, 2015
Learning Node.JS Programming - Nov 10, 2014
App idea to learn new code - Mar 10, 2014
Web and mobile development using Perl, HTML5, CSS3, and JavaScript - May 28, 2013
July 2016 ToDo - Implement Micropub spec in JavaScript editor - Jul 14, 2016
more >>



A     A     A     A     A

© 2013-2017 JotHut - Online notebook

current date: Nov 14, 2024 - 12:22 p.m. EST