Can I use regex to strip out xml whitespace?

Posted on 2006-04-25
Last Modified: 2007-12-19
Hello all,

I'm doing a lot of work with XML, but there are some cases where it would be faster to just deal with the XML data as a string.
Changing it to a string isn't a problem, but there's some whitespace that gets added in between the nodes that's throwing me off.

Can I structure a regular expression that can strip out the whitespace in between the nodes of the string?

I've done simple expressions with 'g' for replace-all commands, but nothing quite like this.

Any assistance would definitely be appreciated.

Question by:Inward_Spiral
    LVL 41

    Expert Comment

    In what language?

      $data =~ s/\s+//g;

      data = data.replace( ' ', '');

      str = new String( str.replaceAll( "\s+", '' );

    Author Comment

    JavaScript, actually.

    I'm pulling in some XML text from the server via AJAX, which sometimes has whitespace (spaces, carriage returns, etc.), and can come in like this sometimes:

    I'd like to strip the string of all spacing between the "><" brackets:

    Does that help?
    LVL 7

    Accepted Solution


    re = /\s+/g;
    data = data.replace(re,"");

    This is just a first pass, a better regex would be
    re = />\s+</g;
    data = x.replace(re,"><");

    So that only whitespace between tags will be caught


    Author Comment

    Thanks Bill, that was exactly what I needed.
    LVL 7

    Expert Comment

    No problem, thanks for the points


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now