当我要替换三个完全相同的单词时,我无法理解为什么我的代码不起作用。假设输入是“www.facebook.com www.facebook.com wwww.facebook.com”
我的输出将是这不是我想要的
<div class="content" style="font-size:13px;">
<a href="www.facebook.com">www.facebook.com</a>
<a href="<a href=" www.facebook.com'=""> www.facebook.com</a>'> www.facebook.com www.facebook.com
</div>
但如果我输入让我们说“www.facebook.com www.google.com wwww.yahoo.com”我得到我想要的
<div class="content" style="font-size:13px;">
<a href="www.facebook.com">www.facebook.com</a>
<a href=" www.google.com"> www.google.com</a>
<a href="wwww.yahoo.com">wwww.yahoo.com</a>
</div>
我实际上想要做的是当用户在文本区域中输入链接并提交它时。该代码将用锚标记替换匹配链接。
这是我的 .js
var wordlink2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
var hyperlink2;
hyperlink2 = content.match(wordlink2);
var $item = $("#ClassroomCommentComment").val();
if (hyperlink2 != null) {
for (x = 0; x < hyperlink2.length; x++) {
var site2 = hyperlink2[x].toString();
var z2 = $item.replace(hyperlink2[x], "<a href='" + site2 + "'>" + site2 + "</a>");
$item = z2;
}
}
我已经对此感到震惊。谁能帮我这个 :(
更准确地说,这是我的功能:
function checkSubmit()
{
var $item = $("#ClassroomCommentComment").val();
var $cont = $("#FileFile").val();
if($item == "")
{
return false;
}
else
{
//$("#ClassroomCommentComment").css('color', 'white');
if (hyperlink != null)
{
for (x= 0; x<hyperlink.length; x++)
{
var site = hyperlink[x].toString();
var z = $item.replace(hyperlink[x],"<a href='" + site + "'>" + site +"</a>" );
$item = z;
}
}
if (hyperlink2 != null)
{
for (x= 0; x<hyperlink2.length; x++)
{
var site2 = hyperlink2[x].toString();
var z2 = $item.replace(hyperlink2[x],"<a href='" + site2 + "'>" + site2 +"</a>" );
$item = z2;
}
}
for (x=0; x<=arraycontroller; x++)
{
var n = $item.replace(fullnamewp[x],"<a href = 'http://www.classoncloud.org/" + userlink[x] + "'>" + fullnamewp[x] +"</a>" );
$item = n;
//$("#ClassroomCommentComment").val(n);
}
if (hash != null)
{
for (x= 0; x<hash.length; x++)
{
var new_item = hash[x].toString().substr(1).split("#");
var b = $item.replace(hash[x],"<a href='http://www.classoncloud.org/results/" + new_item + "'>" + hash[x] +"</a>" );
$item = b;
}
}
arraycontroller = 0;
fullnamewp = [];
userlink = [];
var classroom_id = $("#ClassroomCommentClassroomId").val();
var user_id = $("#ClassroomCommentUserId").val();
var comment = $item;
var image = "<?php echo $profile_pic; ?>";
var name = "<?php echo $current_user_name; ?>";
$.ajax({
type: "POST",
url: '/learns/classroom/',
data: "data[ClassroomComment][classroom_id]=" + classroom_id + "&data[ClassroomComment][user_id]=" + user_id + "&data[ClassroomComment][comment]=" + comment,
dataType: "html",
success: function(data){
var parent_id = data;
$('#comment-list').prepend('<div class="comments"> <a href="'+user_id+'" class="image"><img src="'+image+'" width="50" height="50"> </a><a href="#" class="wall_delete" id='+parent_id+' style="display: none;"></a> <div class="name"> <a href="'+user_id+'" class="close">X</a><a href="#" style="font-size:13px;">'+name+'</a><div class="content" style="font-size:13px;">'+comment+'</div> <div class="time"><div class="star-count"></div><a class="my-star" href="#" id='+parent_id+'>myStar .</a><a class="click" href="#">Comment</a> 1 seconds ago </div></div> <form id="LearnsClassroomForm" accept-charset="utf-8" method="post" action="/learns/classComment"><input type="hidden" name="data[ClassroomComment][parent_id]" action="classroom" value='+parent_id+' id="ClassroomCommentParentId"><input type="hidden" name="data[ClassroomComment][classroom_id]" action="classroom" value="'+classroom_id+'"id="ClassroomCommentClassroomId"><input type="hidden" name="data[ClassroomComment][user_id]" action="classroom" value="'+user_id+'" id="ClassroomCommentUserId"><div class="displaycomment"><textarea name="data[ClassroomComment][comment]" class="textarea" placeholder="Write comment here..." id="ClassroomCommentComment" style="overflow: hidden; word-wrap: break-word; resize: none; height: 18px;"></textarea> <input type="submit" value="Comment" class="submit" style=" margin-right:7px; margin-top:10px;"> </div> </form></div>');
}
});
$("#ClassroomCommentComment").val("");
$("#ClassroomCommentComment").css('color', 'black');
$($(this).parent().parent().find("textarea")).css("height","30px");
return false;
}
}