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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:00:11+00:00 2026-05-26T19:00:11+00:00

I have a variable that contains a long string that represents an XML document.

  • 0

I have a variable that contains a long string that represents an XML document. Within that string, I need to search for every self-closing tag and expand into two matching opening/closing tags. I’m really not sure how to tackle this and would appreciate your advice. At this point, all I know is how to match a self-closing tag via regex: [^<]+?/> Here’s a short example of what I would like to accomplish:

ORIGINAL STRING:

<outer-tag>
    <inner-tag-1>
        <SELF-CLOSING-TAG-1 foo="bar"/>
        <SELF-CLOSING-TAG-2/>
    </inner-tag-1>
    <inner-tag-2>
        <SELF-CLOSING-TAG-3 attr="value"/>
    </inner-tag-2>
</outer-tag>

MODIFIED STRING:

<outer-tag>
    <inner-tag-1>
        <SELF-CLOSING-TAG-1 foo="bar"></SELF-CLOSING-TAG-1>
        <SELF-CLOSING-TAG-2></SELF-CLOSING-TAG-2>
    </inner-tag-1>
    <inner-tag-2>
        <SELF-CLOSING-TAG-3 attr="value"></SELF-CLOSING-TAG-3>
    </inner-tag-2>
</outer-tag>
  • 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-26T19:00:12+00:00Added an answer on May 26, 2026 at 7:00 pm

    I have used the w3 specifications to create a regexp which correctly parses tags in well-formed XML.

    First, select the characters which define the start-tag (per specs). Then, match the remaining characters, excluding possibly trailing spaced and />. Globally replace the matched substrings by
    "<" + starttag + remaining + "></" + starttag + ">". See below:

    //According to the W3 spec: 
    var pattern = /<([:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD][-.0-9\xB7\u0300-\u036F\u0203F-\u2040]*)([^>]*?)\s*?\/>/g;
    string.replace(pattern, '<$1$2></$1>');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a unit test with variable that contains a very long string. Question
I have a variable that contains a long string. (specifically it contains a few
Lets say i have a variable that contains the number of search engine names
I have a variable that contains JSON I need to pass into a template.
I have a string variable that contains the entire HTML of a web page.
I have a variable that contains a 4 byte, network-order IPv4 address (this was
I have a variable that contains : Sometimes array of strings and sometimes array
If I have a variable that contains text information (say taken from a textarea),
I have a variable foo that contains a time, lets say 4pm today, but
I have a 'price' variable that contains some integer number from a MySQL database.

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.