michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: */ michael@0: michael@0: var BUGNUMBER = 587366; michael@0: var summary = "String.prototype.replace with non-regexp searchValue"; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: /* michael@0: * Check that regexp statics are preserved across the whole test. michael@0: * If the engine is trying to cheat by turning stuff into regexps, michael@0: * we should catch it! michael@0: */ michael@0: /(a|(b)|c)+/.exec('abcabc'); michael@0: var before = { michael@0: "source" : RegExp.source, michael@0: "$`": RegExp.leftContext, michael@0: "$'": RegExp.rightContext, michael@0: "$&": RegExp.lastMatch, michael@0: "$1": RegExp.$1, michael@0: "$2": RegExp.$2 michael@0: }; michael@0: michael@0: var text = 'I once was lost but now am found.'; michael@0: var searchValue = 'found'; michael@0: var replaceValue; michael@0: michael@0: /* Lambda substitution. */ michael@0: replaceValue = function(matchStr, matchStart, textStr) { michael@0: assertEq(matchStr, searchValue); michael@0: assertEq(matchStart, 27); michael@0: assertEq(textStr, text); michael@0: return 'not watching that show anymore'; michael@0: } michael@0: var result = text.replace(searchValue, replaceValue); michael@0: assertEq(result, 'I once was lost but now am not watching that show anymore.'); michael@0: michael@0: /* Dollar substitution. */ michael@0: replaceValue = "...wait, where was I again? And where is all my $$$$$$? Oh right, $`$&$'" + michael@0: " But with no $$$$$$"; /* Note the dot is not replaced and trails the end. */ michael@0: result = text.replace(searchValue, replaceValue); michael@0: assertEq(result, 'I once was lost but now am ...wait, where was I again?' + michael@0: ' And where is all my $$$? Oh right, I once was lost but now am found.' + michael@0: ' But with no $$$.'); michael@0: michael@0: /* Missing capture group dollar substitution. */ michael@0: replaceValue = "$1$&$2$'$3"; michael@0: result = text.replace(searchValue, replaceValue); michael@0: assertEq(result, 'I once was lost but now am $1found$2.$3.'); michael@0: michael@0: /* Check RegExp statics haven't been mutated. */ michael@0: for (var ident in before) michael@0: assertEq(RegExp[ident], before[ident]); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("All tests passed!");