From b8fceccb14bda1f4820d184ce16012a6adf3c60d Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 10 Jul 2009 19:42:55 +0000 Subject: [PATCH] * allow RegExp delimiter parameter in before() and after() --- .../filebot/format/ExpressionFormat.global.js | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/source/net/sourceforge/filebot/format/ExpressionFormat.global.js b/source/net/sourceforge/filebot/format/ExpressionFormat.global.js index e95b5600..d5653704 100644 --- a/source/net/sourceforge/filebot/format/ExpressionFormat.global.js +++ b/source/net/sourceforge/filebot/format/ExpressionFormat.global.js @@ -38,7 +38,7 @@ String.prototype.space = function(replacement) { * Return substring before the given delimiter. */ String.prototype.before = function(delimiter) { - var endIndex = this.indexOf(delimiter); + var endIndex = delimiter instanceof RegExp ? this.search(delimiter) : this.indexOf(delimiter); // delimiter was found, return leading substring, else return original value return endIndex >= 0 ? this.substring(0, endIndex) : this; @@ -49,6 +49,16 @@ String.prototype.before = function(delimiter) { * Return substring after the given delimiter. */ String.prototype.after = function(delimiter) { + if (delimiter instanceof RegExp) { + var match = this.match(delimiter); + + if (match == null) + return this; + + // use pattern match as delimiter + delimiter = match[0]; + } + var startIndex = this.indexOf(delimiter); // delimiter was found, return trailing substring, else return original value @@ -56,6 +66,14 @@ String.prototype.after = function(delimiter) { } +/** + * Remove leading and trailing whitespace. + */ +String.prototype.trim = function() { + return this.replace(/^\s+|\s+$/g, ""); +} + + /** * Replace trailing parenthesis including any leading whitespace. *