RegEx tokenize strings with nested quotes

RegEx tokenize strings with nested quotes

by Tony Tubbs

HTML

<button type="button" id="matchBtn">match</button>
<div id="out">

</div>

CSS

textarea {
    width: 100%;   
    height: 150px;
}

textarea.test {
    width: 100%;   
    height: 50px;
}

JavaScript

$(function() {
    var tests = [
        '"completed","email","role","first","last","title","company","SIGIMAGE_1_consent_checkbox","SIGIMAGE_1_consent_dropdown"',
        '"completed", "email", "role", "first", "last", "title", "company", "SIGIMAGE_1_consent_checkbox", "SIGIMAGE_1_consent_dropdown"',
        '"2016-10-04 20:25:05","[email protected]","SIGNER","Sign,","in the ""sig form "" test","","","Checked","Illinois"',
        '"2016-10-04 20:25:05", "[email protected]", "SIGNER", "Sign,", "in the ""sig form "" test", "", "", "Checked","Illinois"'
    ];

    $("#matchBtn").click(function() {
    	var $out = $("#out");
        var   re = /(\".*?\")(,|\n|\r|\r\n|$)/g;
        
        for(var i=0; i < tests.length; i++) {
        	$out.append( $("<h3></h3>").text("Test") );
        	$out.append( $("<textarea class=\"test\"></textarea>").text(tests[i]) );
            $out.append( $("<h5></h5>").text("Matches") );
            var $matches = $("<textarea></textarea>");
            $out.append( $matches );
            match = re.exec(tests[i]);
            while (match != null) {            
            	$matches.append(match[1] + "\r\n");
              match = re.exec(tests[i]);
            }
        }        
    });
});