String.prototype.trim =
function
(char, type) {
if
(char) {
if
(type ==
'left'
) {
return
this
.replace(
new
RegExp(
'^\\'
+char+
'+'
,
'g'
),
''
);
}
else
if
(type ==
'right'
) {
return
this
.replace(
new
RegExp(
'\\'
+char+
'+$'
,
'g'
),
''
);
}
return
this
.replace(
new
RegExp(
'^\\'
+char+
'+|\\'
+char+
'+$'
,
'g'
),
''
);
}
return
this
.replace(/^\s+|\s+$/g,
''
);
};
var
str =
' Ruchee '
;
console.log(
'xxx'
+ str.trim() +
'xxx'
);
str =
' Ruchee '
;
console.log(
'xxx'
+ str.trim(
' '
,
'left'
) +
'xxx'
);
str =
' Ruchee '
;
console.log(
'xxx'
+ str.trim(
' '
,
'right'
) +
'xxx'
);
str =
'/Ruchee/'
;
console.log(str.trim(
'/'
));
str =
'/Ruchee/'
;
console.log(str.trim(
'/'
,
'left'
));
str =
'/Ruchee/'
;
console.log(str.trim(
'/'
,
'right'
));