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

  • Home
  • SEARCH
  • 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 3318886
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:45:34+00:00 2026-05-17T22:45:34+00:00

I’m working on a website project and I have a paragraph containing a list

  • 0

I’m working on a website project and I have a paragraph containing a list of items (it would work great as a ul, but needs to stay a p) that needs to have the first letter of each item bold. I’ve created a function to do this:

function inserter(string, splitter, skip) {
    var array = string.split(splitter);
    var len = array.length;

    for(var i = 1; i < len; i++) {
        var a = array[i];
        var b = '<b>';
        var c = '</b>';
        if(a.substr(0, 3) != skip){ 
            array[i] = splitter + b + a.substr(0,1) + c + a.substr(1);
        } else { 
            array[i] = splitter + a; 
        }
    }
    var strFix = array.join("");
    return strFix;
}

$(function(){
    var text = $(".caps").html();
    text = inserter(text, ': '); //bold the item after ': '
    text = inserter(text, ', ', 'and'); // now bold after the comma ', ' and the skip variable which matches first three letters so the last and doesn't get bold
    text = inserter(text, ', and '); //now for the item after the last 'and'
    $(".caps").html(text);
});

But it needs to be called and the string iterated for every different splitter (which could ruin performance on pages with more than a few of these), and I’m wondering how I could just call it once so all the splitters are looked at during one iteration?

Example page:

http://heidikratzke.com/about.php

When you see the page, you will see that I will be doing this on multiple paragraphs within a jQuery slideshow.

If it doesn’t seem like this will be a performance hit for slower browsers, I’ll leave it as is.

Appreciate any suggestions on how to do it better.

  • 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-05-17T22:45:35+00:00Added an answer on May 17, 2026 at 10:45 pm

    One optimization you could make is to use the splitter you use to split the string into an array to join the array once the operation is finished:

    function inserter(string, splitter, skip) {
        var array = string.split(splitter);
        var len = array.length;
    
        for(var i = 1; i < len; i++) {
            var a = array[i];
            var b = '<b>';
            var c = '</b>';
            if(a.substr(0, 3) != skip){ 
                array[i] = b + a.substr(0,1) + c + a.substr(1);
            } else { 
                array[i] = a; 
            }
        }
        return array.join(splitter);
    
    }
    

    There’s probably more you could do here as well, but this jumped out at me.

    further optimization

    The following gets variable declaration out of the loop:

    function inserter(string, splitter, skip) {
        var array = string.split(splitter);
        var len = array.length;
        var i, a, b='<b>', c='</b>';
    
        for(i = 1; i < len; i++) {
            a = array[i];
            if(a.substr(0, 3) != skip){ 
                array[i] = b + a.substr(0,1) + c + a.substr(1);
            } else { 
                array[i] = a; 
            }
        }
    
        return array.join(splitter);    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.