示例#1
0
文件: RegExp.php 项目: mk-pmb/js2php
RegExp::$protoMethods = array('exec' => function ($str) {
    $self = Func::getContext();
    $str = to_string($str);
    //todo $offset
    $offset = 0;
    $result = preg_match($self->toString(true), $str, $matches, PREG_OFFSET_CAPTURE, $offset);
    if ($result === false) {
        throw new Ex(Error::create('Error executing Regular Expression: ' . $self->toString()));
    }
    if ($result === 0) {
        return Object::$null;
    }
    $index = $matches[0][1];
    $self->set('lastIndex', (double) ($index + strlen($matches[0][0])));
    $arr = new Arr();
    foreach ($matches as $match) {
        $arr->push($match[0]);
    }
    $arr->set('index', (double) $index);
    $arr->set('input', $str);
    return $arr;
}, 'test' => function ($str) {
    $self = Func::getContext();
    $result = preg_match($self->toString(true), to_string($str));
    return $result !== false;
}, 'toString' => function () {
    $self = Func::getContext();
    return $self->toString(false);
});
RegExp::$protoObject = new Object();
RegExp::$protoObject->setMethods(RegExp::$protoMethods, true, false, true);