diff --git a/src/lang/string.js b/src/lang/string.js index 844d6d9..5530ebb 100644 --- a/src/lang/string.js +++ b/src/lang/string.js @@ -39,6 +39,11 @@ Object.extend(String.prototype, (function() { var result = '', source = this, match; replacement = prepareReplacement(replacement); + if (!(pattern.length || pattern.source)) { + replacement = replacement(''); + return replacement + source.split('').join(replacement) + replacement; + } + while (source.length > 0) { if (match = source.match(pattern)) { result += source.slice(0, match.index); diff --git a/test/unit/string_test.js b/test/unit/string_test.js index ab20366..3590579 100644 --- a/test/unit/string_test.js +++ b/test/unit/string_test.js @@ -44,6 +44,11 @@ new Test.Unit.Runner({ 'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'
')); this.assertEqual('ウィメンズ2007
クルーズコレクション', 'ウィメンズ2007\nクルーズコレクション'.gsub('\n','
')); + + this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar', + source.gsub('', 'bar')); + this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar', + source.gsub(new RegExp(''), 'bar')); }, testGsubWithReplacementTemplateString: function() {