/**
 * 反转义字符串，用于处理用户输入
 */ const reverseEscape = ( str ) => JSON . parse ( `{"key": ${ str }}` ) . key ; const escapeDoubleQuote = ( str ) => str . replace ( /"/g , `\\"` ) ; const isNumber = ( text ) => ! isNaN ( Number ( text ) ) ; const string2ReplaceParam = ( str ) : ReplaceParam [ ] => { const brackets = str . split ( /\s*;\s*(?=\()/ ) ; const params = [ ] ; for ( let bracket of brackets ) { const [ regString , newSubStr , fnKey ] = bracket . replace ( /\((\/.*\/[gimsuy]*)\x20*,\x20*"(.*")\)?/ , `$1😎"$2` ) . replace ( /"\x20*,\x20*(\d)\)/g , '"😎$1' ) . split ( "😎" ) ; if ( ( fnKey && ! isNumber ( fnKey ) ) || ( ! fnKey && isNumber ( newSubStr ) ) ) throw "" ; params . push ( { regexp : string2Reg ( regString ) , newSubStr : reverseEscape ( newSubStr ) , fnKey : fnKey ? Number ( fnKey ) : 0 } ) ; } return params ; } ; const string2Reg = ( str ) => { const regParts = str . match ( /^\/(.*?)\/([gimsuy]*)$/ ) ; if ( ! regParts ) throw "" ; return new RegExp ( regParts [ 1 ] , regParts [ 2 ] ) ; } ; const escapeStringRegexp = ( str ) => str . replace ( /[|\\{}()[\]^$+*?.]/g , "\\$&" ) . replace ( /-/g , "\\x2d" ) ; const string2RegArray = ( str ) : RegExp [ ] [ ] => { /**
     * 输入 [/sd/,/sd/];[/sd/,/sd/]
     * /sd/
     * sd => 会转义，相当于普通字符串
     * 输出 [[/sd/]]
     */ if ( /^\(.*\)$/ . test ( str ) ) throw "" ; if ( ! /^\[.*\]$/ . test ( str ) ) return [ [ string2Reg ( /^\/(.*?)\/([gimsuy]*)$/ . test ( str ) ? str : `/${ escapeStringRegexp ( str ) }/g` ) ] ] ; const brackets = str . split ( /\s*;\s*(?=\[)/ ) ; return brackets . map ( bracket => bracket . slice ( 1 , - 1 ) . split ( /\s*,\s*(?=\/)/ ) . map ( str => string2Reg ( str ) ) ) ; } ; const addFlags = ( reg , flags ) => { let allFlags ; if ( flags . length == 1 ) allFlags = reg . flags . includes ( flags ) ? reg . flags : reg . flags + flags ; else allFlags = [ ... new Set ( reg . flags + flags ) ] . filter ( flag => "gimuy" . includes ( flag ) ) . join ( "" ) ; return new RegExp ( reg . source , allFlags ) ; } ; export interface ReplaceParam { regexp : RegExp ; newSubStr : string ; fnKey : number ; } export { string2ReplaceParam , reverseEscape , string2Reg , string2RegArray , escapeDoubleQuote , escapeStringRegexp , addFlags } ;