@@ -21,13 +21,63 @@ String.prototype.$replace = function (c1, c2) {
2121 var regExp = new RegExp ( c1 , "gm" ) ;
2222 return this . replace ( regExp , c2 ) ;
2323} ;
24+ String . prototype . $generateExpFunction = function ( str ) {
25+ var arr = [ ] ;
26+ var orders = [ ] ;
27+ var idx = 0 ;
28+ arr [ 0 ] = "" ;
29+ var i = 0 ;
30+ for ( ; i < str . length ; i ++ ) {
31+ var ch = str . charAt ( i ) ;
32+ if ( i != str . length - 1 && ch == '\\' ) {
33+ i ++ ;
34+ var c = str . charAt ( i ) ;
35+ if ( c == '\\' ) {
36+ arr [ idx ] += '\\' ;
37+ }
38+ arr [ idx ] += c ;
39+ } else if ( i != str . length - 1 && ch == '$' ) {
40+ i ++ ;
41+ orders [ idx ] = parseInt ( str . charAt ( i ) ) ;
42+ idx ++ ;
43+ arr [ idx ] = "" ;
44+ } else if ( ch == '\r' ) {
45+ arr [ idx ] += "\\r" ;
46+ } else if ( ch == '\n' ) {
47+ arr [ idx ] += "\\n" ;
48+ } else if ( ch == '\t' ) {
49+ arr [ idx ] += "\\t" ;
50+ } else if ( ch == '\"' ) {
51+ arr [ idx ] += "\\\"" ;
52+ } else {
53+ arr [ idx ] += ch ;
54+ }
55+ }
56+ var funStr = "f = function (" ;
57+ var max = Math . max . apply ( { } , orders ) ;
58+ for ( i = 0 ; i <= max ; i ++ ) {
59+ funStr += "$" + i ;
60+ if ( i != max ) {
61+ funStr += ", " ;
62+ }
63+ }
64+ funStr += ") { return " ;
65+ for ( i = 0 ; i < arr . length - 1 ; i ++ ) {
66+ funStr += "\"" + arr [ i ] + "\" + $" + orders [ i ] + " + " ;
67+ }
68+ funStr += "\"" + arr [ i ] + "\"; }" ;
69+ var f = null ;
70+ eval ( funStr )
71+ return f ;
72+ } ;
73+
2474String . prototype . replaceAll = function ( exp , str ) {
2575 var regExp = new RegExp ( exp , "gm" ) ;
26- return this . replace ( regExp , str . replace ( / \\ \\ / gm , "\\" ) ) ;
76+ return this . replace ( regExp , this . $generateExpFunction ( str ) ) ;
2777} ;
2878String . prototype . replaceFirst = function ( exp , str ) {
2979 var regExp = new RegExp ( exp , "m" ) ;
30- return this . replace ( regExp , str . replace ( / \\ \\ / gm , "\\" ) ) ;
80+ return this . replace ( regExp , this . $generateExpFunction ( str ) ) ;
3181} ;
3282String . prototype . matches = function ( exp ) {
3383 var regExp = new RegExp ( exp , "gm" ) ;
0 commit comments