Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:07:46+00:00 2026-06-14T14:07:46+00:00

I cannot understand why my code is not working when I am about to

  • 0

I cannot understand why my code is not working when I am about to replace three exact same words. Lets say if an input is “www.facebook.com http://www.facebook.com wwww.facebook.com”

my output would be this which is not what I want

<div class="content" style="font-size:13px;">
<a href="www.facebook.com">www.facebook.com</a>
<a href="&lt;a href=" www.facebook.com'=""> www.facebook.com</a>'&gt; www.facebook.com www.facebook.com
</div>

but if I input lets say “www.facebook.com http://www.google.com wwww.yahoo.com” I get what I want

<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>

What I am actually trying to do is when a user inputs a link in a text area and submits it. The code will replace the match link with an anchor tag.

This is my .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;
    }
}​

I am already blown away with this. Can anyone help me with this 🙁

just an additional this is my function to be more precise:

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&nbsp;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;   

        }   

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:07:47+00:00Added an answer on June 14, 2026 at 2:07 pm

    I think you’re making this more complicated than you need to. The .replace() method accepts a regex to specify what should be replaced, and if you set the g flag on the regex it does a global replace. So you can make the appropriate change to all hyperlinks with a single call to replace as follows:

    var $item = $("#ClassroomCommentComment").val();
    $item = $item.replace(/((^|[^\/])(www\.[\S]+(\b|$)))/gi, '<a href="$1">$1</a>');
    // do something with $item
    

    Demo: http://jsfiddle.net/7vcZd/1/

    There’s no need to first use the .match() method in order to use the result of that match as the first argument for .replace().

    The reason your code was producing the weird output is that you were passing a string as the first parameter to .replace() – when you do that it doesn’t do a global replacement, it just replaces the first instance. Because you did the same replacement in a loop, the second and subsequent replacements were within the <a ... part that you added in the first replacement. If you add a console.log($item) statement to the end of the loop you’ll see what’s happening with each iteration…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

foo\r\nbar.replace(/(foo).+/m, bar) Hello. I can not understand why this code does not replace foo
I am studying a MIPS code that uses procedure but I can not understand
I cannot understand why the code below is giving me this error in firebug
The following code when compiled give this error I cannot understand why this is
I'm new to c++ coming from c#. The following code is not working and
I cannot understand what is the difference between: #define WIDTH 10 and int width
I cannot understand why a call to read after an lseek returns 0 number
I cannot understand the calculation 66 ⊕ fa = 9c. The sum is clearly
I cannot understand how Bing Community site is implemented. Clicking one of the All
I cannot understand the Java memory usage. I have an application which is executed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.