On Sat, 27 Nov 2004 15:56:24 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:
[snip]
str = str.replace(
/(name=)([\"\']?)([\w.:-]+|[^\"]+|[^\']+)\2/i,
'$1$2$3' + stuff + '$2'
);
I just realised that I've reintroduced a problem I initially removed. With
the regular expression above, the second saved match contains either a
quote character, or nothing if the attribute value wasn't quoted in the
first place. When the replacement string is constructed, the result of
this match - a quote or nothing - will be used again, which means that as
it stands, the 'stuff' string would have to conform to the same
restrictions as unquoted values:
[snip]
That is, only letters, numbers, underscores (_), periods (.), colons
(:), and hyphens (-) will be accepted.
Presumably, you're taking a string obtained from innerHTML, modifying it,
and placing it back into the document. That would account for the leniency
you wanted as different browsers normalise the innerHTML string in
different ways.
If the restriction I unintentionally imposed is unacceptable, one way
around it is to use a function, rather than a string, with
String.prototype.replace:
var stuff = '...';
function addStuff() {
var s = arguments[2] || '"';
return arguments[1] + s + arguments[3] + stuff + s;
}
str = str.replace(
/(name=)([\"\']?)([\w.:-]+|[^\"]+|[^\']+)\2/i,
addStuff
);
The only problem here is that IE only supports a function argument with
JScript 5.5+ (IE 5.5+/WinME/WinXP). Though a toString method could be
defined which performs the original operation:
addStuff.toString = function() {
return '$1$2$3' + stuff + '$2';
};
IE normalises its innerHTML strings so that unnecessary quotes are removed
- back to square one.
If you do reach this point, you'll have to abandon my suggestion. The last
recourse would be the String.prototype.split method, however IE's
implementation is broken[1] when used with regular expressions.
Mike
[1] Well, it doesn't conform to ECMA-262, 3rd Ed.
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.