declare var string ; class RegExp { private static loaded = false ; private nativeHandle : any ; private isGlobal : boolean ; private lastIndex : number ; constructor ( private pattern , private flags ? ) { if ( ! RegExp . loaded ) { RegExp . loaded = true ; import pcre2adapter from 'pcre2adapter' ; } if ( pcre2adapter ) { let flagsEnum = 0 ; if ( flags ) { for ( const flag of flags ) { switch ( flag ) { case 'g' : this . isGlobal = true ; break ; case 'i' : flagsEnum |= 1 ; break ; case 'm' : flagsEnum |= 2 ; break ; } } } this . nativeHandle = pcre2adapter . regcomp ( pattern , flagsEnum ) ; } } public test ( t ) { if ( ! t ) { return false ; } if ( this . nativeHandle ) { return pcre2adapter . regtest ( this . nativeHandle , t ) ; } return ! string . match ( t , this . __getLuaPattern ( ) ) ; } public exec ( t ) { if ( ! t ) { return false ; } if ( this . nativeHandle ) { const matchResult = pcre2adapter . regexec ( this . nativeHandle , t , this . lastIndex !== undefined ? this . lastIndex + 1 : null ) ; if ( matchResult ) { this . lastIndex = matchResult . index ; } return matchResult ; } return string . match ( t , this . __getLuaPattern ( ) ) ; } public getPattern ( ) { return this . pattern ; } public __getLuaPattern ( ) { return string . gsub ( this . pattern , '\\' , '%%' ) ; } }