@@ -21,13 +21,63 @@ String.prototype.$replace = function (c1, c2) {
21
21
var regExp = new RegExp ( c1 , "gm" ) ;
22
22
return this . replace ( regExp , c2 ) ;
23
23
} ;
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
+
24
74
String . prototype . replaceAll = function ( exp , str ) {
25
75
var regExp = new RegExp ( exp , "gm" ) ;
26
- return this . replace ( regExp , str . replace ( / \\ \\ / gm , "\\" ) ) ;
76
+ return this . replace ( regExp , this . $generateExpFunction ( str ) ) ;
27
77
} ;
28
78
String . prototype . replaceFirst = function ( exp , str ) {
29
79
var regExp = new RegExp ( exp , "m" ) ;
30
- return this . replace ( regExp , str . replace ( / \\ \\ / gm , "\\" ) ) ;
80
+ return this . replace ( regExp , this . $generateExpFunction ( str ) ) ;
31
81
} ;
32
82
String . prototype . matches = function ( exp ) {
33
83
var regExp = new RegExp ( exp , "gm" ) ;
0 commit comments