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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:42:45+00:00 2026-06-01T11:42:45+00:00

I would like to replace extra spaces (instances of consecutive whitespace characters) with one

  • 0

I would like to replace extra spaces (instances of consecutive whitespace characters) with one space, as long as those extra spaces are not in double or single quotes (or any other enclosures I may want to include).

I saw some similar questions, but I could not find a direct response to my needs above. Thank you!

  • 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-01T11:42:46+00:00Added an answer on June 1, 2026 at 11:42 am

    Hope you’re still looking, or come back to check! This seems to work for me:

    '/\s+((["\']).*?(?=\2)\2)|\s\s+/'
    

    …and replace with  $1

    EDIT

    Also, if you need to allow for escaped quotes like \" or \', you could use this expression:

     '/\s+((["\'])(\\\\\2|(?!\2).)*?(?=\2)\2)|\s\s+/'
    

    It gets a bit stickier if you want to add support for “balanced” quotes like brackets (e.g. () or {})

    END EDIT

    Let me know if you find problems or would like some explanation!


    HOPEFULLY FINAL EDIT AND WARNINGS

    • Potential problem: If a quoted string starts at the beginning of the string variable (or file), it will either not count as a quoted string (and have any whitespace reduced) or it will throw off the whole thing, making anything NOT in quotes get treated as though it was in quotes and vice versa –
      • A potential change that might remedy this is to use the following match expression
      • /(?:^|\s+)((["\'])(\\\\\2|(?!\2).)*?(?=\2)\2)|\s\s+/
      • this replaces \s+ with (?:^|\s+) at the beginning of the expression
      • this will add a space at the beginning of the variable if the string starts with a quote – just trim() or remove that whitespace to continue
    • I seem to have used the “line by line” approach (like sed, if I’m not mistaken) to reach my original results – if you use the “whole file” or “whole string” setting or approach, carriage-return-line-feed seems to count as two whitespace characters (can’t imagine why…), thus turning any newlines into single spaces (unless they are inside quotes and “dot-matches-newline” is used, of course)
      • this could be resolved by replacing the . and \s shorthand character classes with the specific characters you want to match, like the following:
      • /(?:^|[ \t]+)((["\'])(\\\\\2|(?!\2)[\s\S])*?(?=\2)\2)|[ \t]{2,}/
      • this does not require the dot-matches-newline switch and only replaces multiple spaces or tabs – not newlines – with a single space (and of course, only if they are not quoted)

    EXAMPLE

    This link shows an example of the first expression and last expression in use on sample text on http://codepad.viper-7.com

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

Sidebar

Related Questions

I would like to replace blank spaces/white spaces in a string with commas. STR1=This
I would like to replace drop down list (that has on change event that
I would like to replace the first character ' x ' with the number
I would like to replace a part of the image with my image in
I have the following table and would like to replace the string text of
I have this sample string where I would like to replace the star with
in my script I check some files and would like to replace a part
In a text, I would like to replace all occurrences of $word by [$word]($word)
I have a variable myvar = document.getElementById('myid').innerHTML that I would like to replace all
I would like to replace some of the sequences I use for id's in

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.