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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:35:33+00:00 2026-06-01T02:35:33+00:00

I am trying to replace all series of chars with < pre> < /pre>

  • 0

I am trying to replace all series of ” ” chars with “< pre> < /pre>” using a regular expression in Javascript.

So I want to change the 1st line below to the second:

<p>There   should    be    gaps    of    4    chars    tween    each    word</p>
<p>There<pre>   </pre>should<pre>    </pre>be<pre>    </pre>gaps<pre>    </pre>of<pre>    </pre>4<pre>    </pre>chars<pre>    </pre>tween<pre>    </pre>each<pre>    </pre>word</p>

// Or if I cannot place <pre>'s inside a <p> element then replace them with 
// transparent "_" chars
<p>There<font style="color: transparent">____</font>should...</p>

The reason I am doing this is because I am trying to preserve/display space chars and if I place the whole paragraph inside a pre element then word wrapping gets disabled.

Can you help me get my regular expression to place a pre start and end tag at the ends of each group of ” ” chars?

var p = document.getElementById("myP");
var con = p.innerHTML;
con = con.replace(new RegExp("[ ]{2,}"), "<pre> ");
con = con.replace(new RegExp("[ ]{2,}"), " </pre>");
p.innerHTML = con;

// This is incorrect because it removes all the space chars and only leaves one there
// Test    string
// Test<pre> </pre>string // there should be 4 space chars here
  • 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-01T02:35:34+00:00Added an answer on June 1, 2026 at 2:35 am
    con = con.replace(/(?:\s|&nbsp;)+/g, '&nbsp; &nbsp; ');
    

    This has the advantage of matching any number of spaces (yours and appclay’s only matches 2 or more spaces), ensuring exactly 4 spaces (ruakh’s does not enforce the number of spaces), doesn’t mix <pre> and <p>. The disadvantage of all of these is that they are unsafe with HTML attributes in the innerHTML, which may or may not be an issue for you. To solve that would require a parser or a DOM-based solution; if that’s a concern, let me know in a comment and I could explain how to do it more safely in the DOM.

    Edit: I’ve now accounted for other forms of whitespace (e.g. \t or \n) and existing &nbsp; (non-breaking-spaces).

    Edit again, just noticed this:

    The reason I am doing this is because I am trying to preserve/display space chars and if I place the whole paragraph inside a pre element then word wrapping gets disabled.

    If you don’t need to support IE7 and below, you can add this style to the paragraph or its parent: white-space: pre-wrap. Then you could skip all of this text processing.

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

Sidebar

Related Questions

I am trying to replace all German special characters in a Regular Expression. The
I am trying to replace all the #include whatever.h with #include <whatever.h> using find
I've been trying to replace all # in a Excel worksheet using a winform
I'm trying to replace all periods with the degree symbol. Using the each method
I'm trying to replace all the occurrences of a variable in a string using
I'm trying to replace all the letters and spaces after the first two, using
I'm trying to replace all tags with tags using DOMDocument in PHP and almost
I am trying to replace all $ characters in a String expression like this
I'm trying to replace all whitespace characters in each line I'm parsing with a
Using jQuery, I'm trying to replace all links to images (.jpg, .png, .tiff, .gif)

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.