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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:04:54+00:00 2026-05-26T14:04:54+00:00

I would very much appreciate some assistance from the community on replacing a string

  • 0

I would very much appreciate some assistance from the community on replacing a string at xth position intervals, using javascript regex. For example, if the string length is 161 and the replacement text is <br />, regex would replace the string at the 40th, 80th, 120th, and 160th positions with this replacement text. Is this possible using regex?

Thank you very much.

  • 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-26T14:04:54+00:00Added an answer on May 26, 2026 at 2:04 pm

    A method to add <br /> at ever 40th position is by using the following line:

    string = string.replace(/([\S\s]{40})/g , "$1<br />");
    

    If you want to dynamically set the position use:

    var positions = 40;
    var pattern = new RegExp("([\\s\\s]{" + positions + "})", "g");
    string = string.replace(pattern , "$1<br />");
    

    Explanation of the code:

    1. The first argument of the replace function is a RegExp:
      • ([\S\s] = all non-whitespace and white-space characters = every character).
      • {40} = 40 characters
      • The g flag means: global match, ie: match every possible occurence
      • The parentheses inside the RegExp means: Create a group. This group can later be referred by $1 (first group)
    2. The second argument of the replace function contains $1<br />. That is: replace the full match by the first group ($1), and add <br /> to it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey Guys I would very much appreciate some help with the following. We're using
Would very much appreciate some assistance with this issue. I use the following common
I am an SQL rookie and I would very much appreciate some assistance on
Have a problem I'm trying to work through and would very much appreciate some
Would very much appreciate any help or hint on were to go next. I'm
have small problem, and would very much appreciate help :) I should convert byte
I'm a novice-to-intermediate JavaScript/jQuery programmer, so concrete/executable examples would be very much appreciated. My
I'm writing a complex setup/installer application in native C++/MFC. I would very much like
While it would be very convenient to use inline functions at some situations, Are
I'd appreciate some feedback on a particular approach I'm thinking of using. The scenario

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.