Regex: Remove tags with empty elements for xml

I would like to replace all self-closing elements with a long syntax (because my web browser is disconnecting from them).

Example

<iframe src="http://example.com/thing"/>

becomes

<iframe src="http://example.com/thing"></iframe>

I use the python flavor of regex.

+3
source share
3 answers

None of these solutions will contain attributes such as foo = "/">. Try:

s:<([\w\-_]+)((?:[^'">]|'[^']*'|"[^"]*")*)/\s*>:<$1$2></$1>:

Blown up to show details:

<
    ([\w\-_]+)    # tag name
    (
        [^'">]*| # "normal" characters, or
        '[^']*'| # single-quoted string, or
        "[^"]*"  # double-quotes string
    )*
    /\s*         # self-closing
>

This should always work as long as the markup is valid. (You can change this using lazy quantifiers, if you chose so, for example, "[^ ']' => '. *?'.)

+4
source

Use this python regex:

(<(\w+)[^<]*?)/>

@Kinopiko , .

Regex

  • : <
  • : (\ w +)
  • , : [^ <] *?
  • : >

:

\1></\2>
+1

In Perl,

s:(<(\w+)[^>]*?)/>:$1></$2>:

will do it.

0
source