* added String.replacePart()

This commit is contained in:
Reinhard Pointner 2009-06-28 13:44:38 +00:00
parent 3ded6a5628
commit 6d0eb07ec6
1 changed files with 27 additions and 3 deletions

View File

@ -4,6 +4,7 @@ importPackage(java.lang);
// Collection, Scanner, Random, UUID, etc. // Collection, Scanner, Random, UUID, etc.
importPackage(java.util); importPackage(java.util);
/** /**
* Pad strings or numbers with given characters ('0' by default). * 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" * e.g. "The IT Crowd (UK)" -> "The IT Crowd"
* "Bad Wolf (1)" -> "Bad Wolf, Part 1"
*/ */
String.prototype.replaceTrailingBraces = function(replacement) { String.prototype.replaceTrailingBraces = function(replacement) {
// use empty string as default replacement // use empty string as default replacement
@ -45,3 +45,27 @@ String.prototype.replaceTrailingBraces = function(replacement) {
return this.replace(/\s*\(([^\)]*)\)$/, r); 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;
}