/** regexp-parsing-data.js ***/
function xtractReportType1(data) {
    /* Approach 1 to extracting data from a string and putting it in another string
     * Arg: data = source data
     * Return new string
     * This fx is of no value accept as a demo.
     * Watchout for dashes
     */
    var array = data.match(/\b[\S]+\b/g);
    return array[5] + " " + array[0] + " paint retail price: $" + array[2] + " ea.";
}//eof - xtractReportType1

function xtractReportType2(data){
    /* Approach 2 to extracting data from a string and putting it in another string
     * Arg: data = source data
     * Return new string
     * This fx is of no value accept as a demo, and not recommended option.
     */
    data.match(/^(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\b/);
    return RegExp.$6 + " " + RegExp.$1 + " paint retails for $" + RegExp.$3 + " ea.";
}//eof - xtractReportType2

function xtractNums(str){
    /* Create array of numbers in string
     * Arg: str = source string containing numbers
     *        can be mixed with words.
     * Return array of numbers in str
     * Unlike the previous two, you can use this
     */
    return str.match(/\d+/g);
}//eof - xtractNums

function xtractFormattedNums(data) {
    /* Create array of numbers in string (including formatted with commas and decimal)
     * Arg: data = source string containing numbers
     *        can be mixed with words.
     * Return array of numbers in str
     */
    return data.match(/\d+(,\d{3})*(\.\d{1,2})?/g);
}//eof - xtractFormattedNums

function parseUrl(url) {
    /* Parse URL into protocol, host, path, file and hash (and url)
     *    must be URL only
     * Args: url
     * Return object or null if no match
     */

    url = url.replace(/^\s+|\s+$/g, ''); //alltrim

    if (url.match(/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/)) {
        //RegExp['$&'] is null in some browsers s/b original url
        return  {url: RegExp['$&'], protocol: RegExp.$2,host:RegExp.$3,path:RegExp.$4,file:RegExp.$6,hash:RegExp.$7};
    }
    else {
        return null;
    }
}//eof - parseUrl

function parseEmeddedUrl(url) {
    /* Parse URL into protocol, host, path, file and hash (and url)
     *        can be embedded in string
     * Args: url
     * Return object or null if no match
     */
    var e = /((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?/
    if (url.match(e)) {
        //RegExp['$&'] is null in some browsers s/b original url
        return  {url: RegExp['$&'], protocol: RegExp.$2,host:RegExp.$3, path:RegExp.$4,file:RegExp.$6,hash:RegExp.$7};
    }
    else {
        return null;
    }
}//eof - parseEmeddedUrl

function xtractFile(data){
    /* Separate path and filename.ext
     * Arg: string with path and filename
     * Return: object
     */
    if (data.match(/(.*)\/([^\/\\]+\.\w+)$/)) {
        return {path: RegExp.$1, file: RegExp.$2};
    }
    else {
        return {path: "", file: ""};
    }
}//eof - xtractFile

function xtractFile_sans(data){
    /* Separate path and filename leaving extention off
     * Assumes DOS style with only one dot.
     * Arg: string with path and filename
     * Return: object
     */
    if (data.match(/(.*)\/([^\/\\]+)\.\w+$/)) {
        return {path: RegExp.$1, file: RegExp.$2};
    }
    else {
        return {path: "", file: ""};
    }
}//eof - xtractFile_sans

function xtractFile-ext1(data){
    /* Parses filename and extension
     *
     * Returns Object
     */
    data = data.replace(/^\s|\s$/g, "");

    if (/\.\w+$/.test(data)) {
        var m = data.match(/([^\/\\]+)\.(\w+)$/);
        if (m)
            return {filename: m[1], ext: m[2]};
        else
            return {filename: "no file name", ext:null};
    } else {
        var m = data.match(/([^\/\\]+)$/);
        if (m)
            return {filename: m[1], ext: null};
        else
            return {filename: "no file name", ext:null};
    }
}//eof - xtractFile-ext1

function xtractFile-ext2(data){
    /* Parses filename and extension
     *
     * Returns Object
     */
    data = data.replace(/^\s|\s$/g, ""); //trims string

    if (/\.\w+$/.test(data)) }
        if (data.match(/([^\/\\]+)\.(\w+)$/) )
            return {filename: RegExp.$1, ext: RegExp.$2};
        else
            return {filename: "no file name", ext:null};
    }
    else {
        if (data.match(/([^\/\\]+)$/) )
            return {filename: RegExp.$1, ext: null};
        else
            return {filename: "no file name", ext:null};
    }
}//eof - xtractFile-ext2

function xtractFile-ext4type(data){
    /* Parses filename and extension
     * for specified extenstions
     *
     * Returns Object
     */
    data = data.replace(/^\s|\s$/g, ""); //trims string

    if (data.match(/([^\/\\]+)\.(asp|html|htm|shtml|php)$/i) )
        return {filename: RegExp.$1, ext: RegExp.$2};
    else
        return {filename: "invalid file type", ext: null};
}//eof - xtractFile-ext4type


