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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:40:34+00:00 2026-05-24T07:40:34+00:00

I am allowing my users to wrap words with *, /, _, and –

  • 0

I am allowing my users to wrap words with “*”, “/”, “_”, and “-” as a shorthand way to indicate they’d like to bold, italicize, underline, or strikethrough their text. Unfortunately, when the page is filled with text using this markup, I’m seeing a noticeable (borderline acceptable) slow down.

Here’s the JavaScript I wrote to handle this task. Can you please provide feedback on how I could speed things up?

function handleContentFormatting(content) {
    content = handleLineBreaks(content);

    var bold_object = {'regex': /\*(.|\n)+?\*/i, 'open': '<b>', 'close': '</b>'};
    var italic_object = {'regex': /\/(?!\D>|>)(.|\n)+?\//i, 'open': '<i>', 'close': '</i>'};
    var underline_object = {'regex': /\_(.|\n)+?\_/i, 'open': '<u>', 'close': '</u>'};
    var strikethrough_object = {'regex': /\-(.|\n)+?\-/i, 'open': '<del>', 'close': '</del>'};

    var format_objects = [bold_object, italic_object, underline_object, strikethrough_object];

    for( obj in format_objects ) {
        content = handleTextFormatIndicators(content, format_objects[obj]);
    }

    return content;
}

//@param obj --- an object with 3 properties:
//      1.) the regex to search with
//      2.) the opening HTML tag that will replace the opening format indicator
//      3.) the closing HTML tag that will replace the closing format indicator
function handleTextFormatIndicators(content, obj) {
    while(content.search(obj.regex) > -1) {
        var matches = content.match(obj.regex);
        if( matches && matches.length > 0) {
            var new_segment = obj.open + matches[0].slice(1,matches[0].length-1) + obj.close;
            content = content.replace(matches[0],new_segment);
        }
    }
    return content;
}
  • 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-24T07:40:35+00:00Added an answer on May 24, 2026 at 7:40 am
    1. Change your regex with the flags /ig and remove the while loop.

    2. Change your for(obj in format_objects) loop with a normal for loop, because format_objects is an array.


    Update

    Okay, I took the time to write an even faster and simplified solution, based on your code:

    function handleContentFormatting(content) {
        content = handleLineBreaks(content);
    
        var bold_object = {'regex': /\*([^*]+)\*/ig, 'replace': '<b>$1</b>'},
            italic_object = {'regex': /\/(?!\D>|>)([^\/]+)\//ig, 'replace': '<i>$1</i>'},
            underline_object = {'regex': /\_([^_]+)\_/ig, 'replace': '<u>$1</u>'},
            strikethrough_object = {'regex': /\-([^-]+)\-/ig, 'replace': '<del>$1</del>'};
    
        var format_objects = [bold_object, italic_object, underline_object, strikethrough_object],
            i = 0, foObjSize = format_objects.length;
    
        for( i; i < foObjSize; i++ ) {
            content = handleTextFormatIndicators(content, format_objects[i]);
        }
    
        return content;
    }
    
    //@param obj --- an object with 2 properties:
    //      1.) the regex to search with
    //      2.) the replace string
    function handleTextFormatIndicators(content, obj) {
        return content.replace(obj.regex, obj.replace);
    }
    

    Here is a demo.

    This will work with nested and/or not nested formatting boundaries. You can omit the function handleTextFormatIndicators altogether if you want to, and do the replacements inline inside handleContentFormatting.

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

Sidebar

Related Questions

I'd like to use embedded IronPython as a way of allowing users to script
I am allowing users to upvote an item. When they click the up arrow,
I have created an application where I am allowing users to log their Workouts.
On my site, I'm allowing users to create pages of content, and I'd like
In our web application, I am allowing users to edit their profiles, using a
Suppose I am allowing users to vote only if they are registered users. I
In my application I am allowing users to upload their css style sheets so
I am allowing users to rotate images using jQuery rotate. However, when they do
I am allowing users to embed videos on their page, but just in case
I am looking at allowing users to enter JavaScript to specify some logic in

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.