Subversion Repositories JSX

Compare Revisions

Last modification

Ignore whitespace Rev 496 → Rev 497

/trunk/regexp.js
1044,6 → 1044,9
 
this.value = String(s);
}.extend(String, (function () {
var _replace = String.prototype.replace;
var _getDataObject = jsx.object.getDataObject;
 
function _toString ()
{
return this.value;
1055,7 → 1058,6
* of jsx.regexp.RegExp if possible
*
* @function
* @memberOf jsx.regexp.String#prototype
*/
match: (function () {
var rxLeadingGroups, rxNonWordChars;
1119,6 → 1121,62
}()),
 
/**
* Replaces matches in a string, and returns the new string.
*
* Different to {@link String.prototype.replace()},
* this methods also allows you to refer to backreferences
* by name. In a String-like object, you may use
* <code>"${name}"</code>, and in a replacement function
* you may use <code>this.groups["name"]</code>.
*
* NOTE: Because of the latter the replacement function
* is called as a method of this object, not of
* the Global Object anymore. The <code>groups</code>
* property of this object is retained; that is, the last
* arguments to this method can be found in there.
* (Arguments and return value of the replacement function
* still work as specified in ECMAScript.)
*
* @memberOf jsx.regexp.String.prototype
* @param {jsx.regexp.RegExp|RegExp|String} expression
* @param {String|Function} replacement
* @return {string}
* @see String.prototype.replace
*/
replace: function (expression, replacement) {
if (jsx.regexp.RegExp.isInstance(expression))
{
var groups = expression.groups;
var len = groups.length;
 
if ((typeof replacement) == "function")
{
var me = this;
return _replace.call(this, expression, function () {
me.groups = _getDataObject();
for (var i = 1; i <= len; ++i)
{
me.groups[groups[i]] = arguments[i];
}
 
return replacement.apply(me, arguments);
});
}
 
for (var i = 1; i <= len; ++i)
{
/* replace "${name}" with "${index}" */
replacement = _replace.call(
replacement,
new RegExp("\\$\\{" + groups[i] + "\\}", "g"),
"$" + i);
}
}
 
return _replace.call(this, expression, replacement);
},
 
/**
* Returns this object's encapsulated string value
*/
toString: _toString,
/trunk/test/regexp-test.js
770,7 → 770,7
},
{
name: "PCRE named subpatterns with modifiers: <code>(?sx'name'…)</code>"
+ " (Python style)",
+ " (Perl style)",
code: function () {
var rx = new RegExp2("(?s'foo'.)");
assert(rx.source === "([\\S\\s])");
779,6 → 779,16
}
},
{
name: "PCRE named subpatterns with modifiers: <code>(?sx<name>…)</code>"
+ " (Python style)",
code: function () {
var rx = new RegExp2("(?s<foo>.)");
assert(rx.source === "([\\S\\s])");
assert(rx.groups[1] === "foo");
assert(rx.names["foo"] === 1);
}
},
{
name: "Unicode mode: <code>unicodeMode</code> property",
code: function () {
assertFalse(new RegExp2("\\w", "").unicodeMode);
913,6 → 923,14
}
},
{
feature: 'jsx.regexp.RegExp(…, "su"))',
desc: "Unicode mode: <code>.</code> works with PCRE_DOTALL",
code: function () {
var rx = new RegExp2("x.y", "su");
assert(rx.source === "x[\\S\\s]y");
}
},
{
feature: "jsx.regexp.RegExp.exec(jsx.regexp.RegExp, String)",
desc: "Unicode mode: <code>\\b</code> match"
+ " before non-ASCII letter is trimmed",
953,12 → 971,24
}
},
{
feature: 'jsx.regexp.RegExp(…, "su"))',
desc: "Unicode mode: <code>.</code> works with PCRE_DOTALL",
code: function () {
var rx = new RegExp2("x.y", "su");
assert(rx.source === "x[\\S\\s]y");
}
feature: 'jsx.regexp.String.prototype.replace(jsx.regexp.RegExp), String)',
desc: "Named backreferences are supported",
code: function () {
var rx = new RegExp2(" (?'foo'ä) ö", "g");
var s = new String2(" ä ö").replace(rx, "${foo}");
assert(s === "ä");
}
},
{
feature: 'jsx.regexp.String.prototype.replace(jsx.regexp.RegExp, Function)',
desc: "Named backreferences are supported",
code: function () {
var rx = new RegExp2(" (?'foo'ä) ö", "g");
var s = new String2(" ä ö").replace(rx, function () {
return this.groups["foo"];
});
assert(s === "ä");
}
}
]
});