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 8919239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:58:08+00:00 2026-06-15T05:58:08+00:00

My goal it to loop through a set of given elements, and replace there

  • 0

My goal it to loop through a set of given elements, and replace there inner HTML with a linkifying Regex so I can convert HTML text in the form of http://*.*/* into <a href="http://*.*/*" target="_blank">http://*.*/*</a>

So I’m running a bit of vanilla javascript:

for (var i = 0; i < document.getElementsByClassName('title').length; i++) {
    var title = document.getElementsByClassName('title')[i]
    title.innerHTML = title.innerHTML.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,"<a target='_blank' href='$1'>$1</a>")
}

Here’s just the RegExp I’m using:

/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig

So, why on earth would this loop cause the browser to hang? The loop is over text no longer than 256 characters and there are usually between 5 and 30 .title elements, definitely not the levels of data that would crash/hang a browser. I’ve only experienced it in Chrome/Safari, unsure if it happens in Firefox/Opera or not.

  • 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-15T05:58:09+00:00Added an answer on June 15, 2026 at 5:58 am

    Try storing results.

    var titles = document.querySelectorAll(".title");
    // querySelectorAll is supported in slightly more browsers than getElementsByClassName
    var l = titles.length, i, title;
    for( i=0; i<l; i++) {
        title = titles[i];
        title.innerHTML = title.innerHTML.replace(/..../,'....');
    }
    

    If the hanging continues, it’s probably because the regex is matching stuff you’ve already replaced. Try adding a negative lookahead to ensure there is no single quote immediately after the URL you are matching. (?=!')

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

Sidebar

Related Questions

The goal here is to loop through the multidimensional array below and find user's
The goal: Connect to database using php to extract results Loop through results Store
I am using VB .Net to loop through a regex Match and generate a
Goal : I wants when I drag image it become fade so we can
Goal: Gain datatype date with year and month Problem: Need to help to convert
I'm looping through an array, and during each iteration of the loop, I'm calling
I have a loop going through each of my textboxes in Form1 and getting
I'm trying to loop through a bunch of checkboxes i have created on my
So I am trying to set multiple variables with a For Loop in a
So I have this basic html form. My goal is to pass the values

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.