Javascript 帮助用超链接替换文本表单字段中的链接

Javascript 帮助用超链接替换文本表单字段中的链接,javascript,regex,coldfusion,Javascript,Regex,Coldfusion,我对正则表达式没有太多经验,但我认为这就是我需要使用的。我在coldfusion中有一个页面,我正在使用ajax的几个函数提交信息。其中一个表单字段是“注释”。我想能够找到任何在评论字段的链接,即:并替换为一个工作链接。谢谢你能提供的任何帮助。功能如下: <code> function AddComment(reqid) { var Comment = ''; if(document.getElementById('Comment').value != "")

我对正则表达式没有太多经验,但我认为这就是我需要使用的。我在coldfusion中有一个页面,我正在使用ajax的几个函数提交信息。其中一个表单字段是“注释”。我想能够找到任何在评论字段的链接,即:并替换为一个工作链接。谢谢你能提供的任何帮助。功能如下:

<code>
function AddComment(reqid)
{
    var Comment = '';

    if(document.getElementById('Comment').value != "")
    {
        Comment = document.getElementById('Comment').value;     

        request = getRequest();
        if (!request)
         alert("Error initializing XMLHttpRequest!");

        var url = "#webroot#view-requests-action.cfm?id=" + escape(reqid) + '&Comment=' + escape(Comment) + '&section=' + 'addcomm';
    //alert(url) 
    //return;
        request.open("GET", url, false);
        request.send(null);
        window.location="view-requests.cfm?id=#url.id#&panel=0";
    }
    else
    {
    window.location="view-requests.cfm?id=#url.id#&panel=0";
    }
}   
</code>

这将通过JavaScript实现

sampleText = "Hello World! http://www.google.com";
function InsertLinks(message)
{
    var words = message.split(" ");
    for (var i = 0; i < words.length; i++)
    {
        if (words[i].indexOf("http:") >= 0)
        {
            words[i] = '<a href="' + words[i] + '">' + words[i] + "</a>";
        }
    }
    return words.join(" ");
}       

document.getElementById("test").innerHTML = InsertLinks(sampleText);

这将通过JavaScript实现

sampleText = "Hello World! http://www.google.com";
function InsertLinks(message)
{
    var words = message.split(" ");
    for (var i = 0; i < words.length; i++)
    {
        if (words[i].indexOf("http:") >= 0)
        {
            words[i] = '<a href="' + words[i] + '">' + words[i] + "</a>";
        }
    }
    return words.join(" ");
}       

document.getElementById("test").innerHTML = InsertLinks(sampleText);

谢谢这是有道理的,但我在使用当前代码时遇到了问题。上面的代码示例要求url采用“http:then url”格式。要使用此代码,只需将函数粘贴到库或页面中,无论您在哪里将此字符串“输出”到页面,都可以将其替换为InsertLinks(yourVariableHere)。谢谢。这是有道理的,但我在使用当前代码时遇到了问题。上面的代码示例要求url采用“http:then url”格式。要使用此代码,只需将其粘贴到库或页面中的函数中,无论您在哪里将此字符串“输出”到页面,都可以将其替换为InsertLinks(yourVariableHere)。
Comment = InsertLinks(document.getElementById('Comment').value);