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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:35:17+00:00 2026-05-12T10:35:17+00:00

trying to use regex to replace any white space with  , inside of example

  • 0

trying to use regex to replace any white space with “ ”, inside of example html

<span someattr="a">and some words with spaces</span>

It’s a desktop app and this html is coming to/from a third party control and don’t have the luxury of working with any type of html parsing so am stuck with regex

I can’t seem come up with a regex that would just match any whitespace inside any number of span tags.

Thanks

  • 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-12T10:35:17+00:00Added an answer on May 12, 2026 at 10:35 am

    This could potentially be very slow with very large strings.

    But this works:

    (?<=\<span[^>]*>[^<]+)\s(?=[^<]+\</span>)
    

    With a replacement string of:

    &nbsp;

    The reason I say it might be slow is that it’s having to find the whitespace (\s) and then search towards the left and to the right to see if it’s surrounded by a span tag. And it’ll have to do the same thing for every character of whitespace individually. But I believe this should work reliably as long as your HTML is well-formed and you aren’t dealing with nested span tags.

    And by the way, since this is for .NET you can use Regex Hero to build the code for you:

    string strRegex = "(?<=\<span[^>]*>[^<]+)\s(?=[^<]+\</span>)";
    RegexOptions myRegexOptions = RegexOptions.None;
    Regex myRegex = new Regex(strRegex, myRegexOptions);
    string strTargetString = "<span someattr=\"a\">and some words with spaces</span>";
    string strReplace = "&nbsp;";
    
    return myRegex.Replace(strTargetString, strReplace);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a search and replace regex that will detect whether HTML
I am trying to replace strings in brackets in some html string. When I
I'm trying to get a regex which is able to find the following part
I'm trying to write a parser for the EDI data format, which is just
I have to use tinymce as a wysiwyg editor in a CMS to populate
I've been searching on this but am coming up a little short on exactly
I'm in the process of updating a program that fixes subtitles. Till now I
I have a List of words I want to ignore like this one :
I have a string that may contains Twitter hashtags. I'd like to strip it
I want to process a String, in which I want to find multiple strings,

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.