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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:52:17+00:00 2026-06-12T16:52:17+00:00

I am looking for help to create a regular expression to replace all of

  • 0

I am looking for help to create a regular expression to replace all of the last spaces within a specific tag(eg. <p>) to be &nbsp; instead. To quickly fix all widows in a massive html document.

For example;

    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus suscipit
    dolor a felis blandit sodales. Donec lectus justo, convallis vitae euismod sit.
    Nullam et tristique&nbsp;dui.</p> 
    <p>Nullam accumsan pellentesque pretium. Morbi tempor egestas lectus, 
    a eleifend enim aliquet varius. Vivamus vitae semper&nbsp;tortor.</p>

I found this example at http://www.petefreitag.com/item/580.cfm

ReReplace(text, " ([^ ]+\r?\n)", "&nbsp;\1", "ALL")

But it adds it to every last space in every element.

Thanks also any advice on how to improve how i wrote this question would be awesome

  • 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-12T16:52:18+00:00Added an answer on June 12, 2026 at 4:52 pm

    This problem is a little harder than it looks, since you may have <p> elements with no words, with only one word, or multiple words, or plenty of whitespace before the ending tag </p>. You may even have nested elements within the paragraph element, making what you think might be orphan words not be orphans at all. To make things even more complicated, in many versions of HTML, the ending tag </p> is actually optional.

    For these reasons, it is recommended to use an HTML parser, and not just process your HTML file with a regex.

    ONLY if you know that all <p> elements are closed, and there are NO nested elements within the <p> elements, and ALL <p> elements have more than one word, you can get away with replacing

    (\S)\s+(\S+)\s*</p>
    

    with

    \1&nbsp;\2</p>
    

    You can parenthesize the last \s* and add a \3 to the replacement string if you want to keep the spacing before the end tag if you like.

    I’d be careful before doing something like this without an HTML parser though.

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

Sidebar

Related Questions

I am looking for help in how to create a regular expression that extracts
I am trying to create a proper regular expression to find all anchors in
I'm looking for help to create a query, to solve the following problem: Let's
Looking for some help! I need to split a string at the last occurrence
looking for some help with images referenced within the stylesheet. I have no problems
Now I am looking for your help to create index on these. Now this
I am looking for anyone that can help create some source and a tutorial
I am new to regular expressions. I am looking for something that can help
Looking for some help on the best way to create a custom UIActivityIndicator. I
I'm looking for a GUI to help create and manage keystores for Android development.

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.