30
回调函数需要使用回调函数来代替<wiki>this page</wiki>
到<a href='wiki/this_page'>this page</a>
:更换比赛
text = text.replace(/<wiki>(.+?)<\/wiki>/g, function(match)
{
return "<a href='wiki/"+match.replace(/ /g, '_')+"'>"+match+"</a>";
}
);
结果是标签<wiki>
被保留(全场比赛) - <a href='wiki/<wiki>this_page</wiki>'><wiki>this page</wiki></a>
有没有办法让匹配[0],与[preg_replace_callback()
]中的[1]匹配?
工作。现在我知道JavaScript引用是 – Qiao 2010-08-03 11:12:21
我认为答案没问题,但它太简单了。同样,匹配函数的最后两个参数的顺序似乎不正确。 请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter ,其中解释了匹配函数接收的参数数目不定,具体取决于“括号内的子匹配[es]”的数量。 – user2367418 2017-09-11 19:45:00