find: function()

in xooki.js [278:310]


    find: function(/*string*/str, /*string or regexp*/exp, /*number, optional*/from) {
        // find an expression (string or regexp) in a string, from an optional index
        // the object returned has two properties:
        // begin: the index in str of the matching find
        // end: the index in str of the end of the matching find
        // returns null if no match is found
        if (typeof from != "number") {
            from = 0;
        }
        if (typeof exp == "string") {
            var result = {};
            result.begin = str.indexOf(exp,from);
            if (result.begin >= 0) {
                result.end = result.begin + exp.length;
                return result;
            }
        } else {
            var m;
            if (from > 0) {
                // I haven't found any other way to start from the given index
                m = exp.exec(str.substring(from));
            } else {
                m = exp.exec(str);
            }
            if (m != null) {                
                var result = {};
                result.begin = m.index + from;
                result.end = result.begin + m[0].length;
                return result;
            }
        }
        return null;
    },