From 6d0eb07ec64f19e0533a0536c45be6d6f7a8745b Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 28 Jun 2009 13:44:38 +0000 Subject: [PATCH] * added String.replacePart() --- .../filebot/format/ExpressionFormat.global.js | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/source/net/sourceforge/filebot/format/ExpressionFormat.global.js b/source/net/sourceforge/filebot/format/ExpressionFormat.global.js index 817f1759..901d2cb2 100644 --- a/source/net/sourceforge/filebot/format/ExpressionFormat.global.js +++ b/source/net/sourceforge/filebot/format/ExpressionFormat.global.js @@ -4,6 +4,7 @@ importPackage(java.lang); // Collection, Scanner, Random, UUID, etc. importPackage(java.util); + /** * Pad strings or numbers with given characters ('0' by default). * @@ -34,10 +35,9 @@ String.prototype.space = function(replacement) { /** - * Remove trailing parenthesis including any leading whitespace. + * Replace trailing parenthesis including any leading whitespace. * - * e.g. "Doctor Who (2005)" -> "Doctor Who" - * "Bad Wolf (1)" -> "Bad Wolf, Part 1" + * e.g. "The IT Crowd (UK)" -> "The IT Crowd" */ String.prototype.replaceTrailingBraces = function(replacement) { // use empty string as default replacement @@ -45,3 +45,27 @@ String.prototype.replaceTrailingBraces = function(replacement) { return this.replace(/\s*\(([^\)]*)\)$/, r); } + + +/** + * Replace 'part section'. + * + * e.g. "Today Is the Day: Part 1" -> "Today Is the Day, Part 1" + * "Today Is the Day (1)" -> "Today Is the Day, Part 1" + */ +String.prototype.replacePart = function (replacement) { + // use empty string as default replacement + var r = replacement ? replacement : ""; + + // handle '(n)' and ': Part n' syntax + var pattern = [/\s*\((\w+)\)$/i, /\s*\W? Part (\w+)$/i]; + + for (var i = 0; i < pattern.length; i++) { + if (pattern[i].test(this)) { + return this.replace(pattern[i], r); + } + } + + // no pattern matches, nothing to replace + return this; +}