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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:44:52+00:00 2026-05-13T23:44:52+00:00

I need a regular expression to solve the following problem (links to similar problems

  • 0

I need a regular expression to solve the following problem (links to similar problems is also appreciated, related tutorials etc.):

"__some_words_a_b___" => "__some words a b___"
"____" => "____"
"some___words" => "some   words"

So I want underscores between words to be replaced with space and keep leading and trailing underscores. I found this:

^[ \t]+|[ \t]+$

and I guess it most be something like that. I will use it in jQuery, Java (stdlibs) and maybe XSLT.

Addition:
The sentences do not necessarily start with underscores or ends with underscores. It is also possible that a sentence ain’t containing underscores at all. Multiple underscores should render to multiple spaces

Best regards
Lasse Espeholt

  • 1 1 Answer
  • 1 View
  • 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-13T23:44:53+00:00Added an answer on May 13, 2026 at 11:44 pm

    This should work in Javascript:

    var newString = oldString.replace(/([^_].*?)_(?=[^_|^\s])/g,"$1 ");
    

    Edit: if you have whitespace in the string already, might need something like this added:

    var newString = oldString.replace(/([^_|\s].*?)_(?=[^_|^s])/g,"$1 ");
    

    Any other edge cases I forgot? 🙂 Oh yeah, another edge case. Keep the ending underscore if followed by whitespace (like a newline, end of line, etc).

    edit: Alternate solution for if the number of underscores in between words>1

    var arrayString = oldString.replace(/^(_+)(.*?)(_+)$/g,"$1;$2;$3");
    var a = arrayString.split(";");
    var newString = a[0]+a[1].replace(/_/g," ")+a[2];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.