假设我们有一个注释文本区域,用户可以输入验证码:的jQuery/JavaScript的 - 字符串替换
[quote="comment-1"]
我如何可以替换代码的形式从<div id="comment-1">
实际的HTML内容提交过吗?
假设我们有一个注释文本区域,用户可以输入验证码:的jQuery/JavaScript的 - 字符串替换
[quote="comment-1"]
我如何可以替换代码的形式从<div id="comment-1">
实际的HTML内容提交过吗?
你可以尝试这样的事:
var text = $('textarea').val();
text = text.replace(/\[quote="comment-(\d+)"\]/g, function(str,p1) { return $('#comment-' + p1).text(); });
$('textarea').val(text);
应该匹配agains在你给的格式的任何编号报价。
您可以使用正则表达式:
text = text.replace(/\[quote="([a-z0-9-]+)"]/gi,
function(s, id) { return $('#' + id).text(); }
);
如果我理解正确的话,你要替换类似“[报价=‘评论-1’]”与“”。
在JavaScript:
// Where textarea is the reference to the textarea, as returned by document.getElementById
var text = textarea.value;
text = text.replace(/\[quote\="(comment\-1)"\]/g, '<div id="$1">');
在jQuery中:
// Where textarea is the reference to the textarea, as returned by $()
var text = textarea.val();
text = text.replace(/\[quote\="(comment\-1)"\]/, '<div id="$1">');
希望这有助于!
作品,谢谢:P – Alex 2010-07-07 00:22:48