CDATA
- 块非常适合将大块HTML或CSS编码为字符串。但是,我无法弄清楚如何在一个变量值中使用变量值。
例如,请考虑以下JavaScript代码:
var FullName = "Friedrich Hayek";
var ProfileCode = (<><![CDATA[
<div id="BigHonkingChunkO_HTML">
...Lot's o' code...
Name: $FullName$
Birth: $Birthdate$
...Lot's o' code...
... $FullName$ ...
...Lot's o' code...
</div>
]]></>).toString ();
如何让$FullName$
呈现为“Friedrich Hayek”而不是“$ FullName $”?
请注意,有多个变量,每个变量可以在CDATA块中使用几次。
替代代码示例:
var UserColorPref = "red";
var UI_CSS = (<><![CDATA[
body {
color: $UserColorPref$;
}
]]></>).toString ();
希望将颜色属性设置为red
。
答案 0 :(得分:5)
它够优雅吗?
function replaceVars(content, vars)
{
return content.replace(/\$(\w+)\$/g, function($0, $1)
{
return ($1 in vars ? vars[$1] : $0);
});
}
ProfileCode = replaceVars(ProfileCode, {"FullName" : "Friedrich Hayek"});
如果关联键不重要,您可以随时选择使用:
sprintf或vsprintf
修改强>
如何使第二个参数可选?
function replaceVars(content, scope) {
if (!scope || typeof scope != "object") {
scope = this;
}
return content.replace(/\$(\w+)\$/g, function($0, $1) {
return ($1 in scope ? scope[$1] : $0);
});
}
// example1
var FullName = "Friedrich Hayek";
ProfileCode = replaceVars(ProfileCode);
// example2
var vars = {"FullName" : "Friedrich Hayek"};
ProfileCode = replaceVars(ProfileCode, vars);
答案 1 :(得分:3)
ProfileCode=ProfileCode.replace('$FullName$',FullName);
答案 2 :(得分:1)
在搜索the CDATA spec和this "CDATA Confusion" article之后,CDATA部分似乎将所有内容视为纯文本,但字符数据实体和部分结束标记(]]>
)除外。例如,
var x = $('<!DOCTYPE X[<!ENTITY foo "BAR">]><z> cc A &foo;</z>');
console.log ($(x, 'z').text() );
收益率:]> cc A &foo;
因此,CDATA部分无法进行变量替换。我们能做的最好的事情就是开始和停止这些部分,如下所示:
var FullName = "Friedrich Hayek";
var ProfileCode = (<><![CDATA[
<div id="BigHonkingChunkO_HTML">
...Lot's o' code...
Name: ]]></>).toString () + FullName+ (<><![CDATA[
...Lot's o' code...
</div>
]]></>).toString ();
console.log (ProfileCode);
- 这显然是不可接受的。
它不会帮助任何寻找CDATA解决方案的人(根据规范,我们现在知道这是不可能的)。但是,由于我们只是使用CDATA作为生成复杂字符串的方法,因此我们可以根据Ratna Dinakar的回答清理字符串。
我们最终使用的功能是:
function sSetVarValues (sSrcStr, sReplaceList /* , Variable */)
/*--- function sSetVarValues takes a string and substitutes marked
locations with the values of the variables represented.
Conceptually, sSetVarValues() operates a little like sprintf().
Parameters:
sSrcStr -- The source string to be replaced.
sReplaceList -- A string containing a comma-separated list of variable
names expected in the raw string. For example, if
sReplaceList was "Var_A", we would expect (but not require)
that sSrcStr contained one or more "$Var_A$" substrings.
*Variable* -- A variable-length set of parameters, containing the values
of the variables specified in sReplaceList. For example,
if sReplaceList was "Var_A, Var_B, Var_C", then there better
be 3 parameters after sReplaceList in the function call.
Returns: The replaced string.
*/
{
if (!sSrcStr) return null;
if (!sReplaceList) return null;
var aReplaceList = sReplaceList.split (/,\s?/);
for (var J = aReplaceList.length-1; J >= 0; --J)
{
var zRepVar = new RegExp ('\\$' + aReplaceList[J] + '\\$', "g");
sSrcStr = sSrcStr.replace (zRepVar, arguments[J+2]);
}
return sSrcStr;
}
使用示例:
var AAA = 'first';
var BBB = 'second';
var CCC = 'third';
var Before = "1 is $AAA$, 2 is $BBB$, 3 is $CCC$";
var After = sSetVarValues (Before, "AAA, BBB, CCC", AAA, BBB, CCC);
console.log (Before);
console.log (After);
<强>收率:强>
1 is $AAA$, 2 is $BBB$, 3 is $CCC$ 1 is first, 2 is second, 3 is third