In the code below, $html
is a string of HTML. The code captures the full match and the capturing group for each match in a list. Then, if the list is not empty, it iterates through the list to replace the span
tags with em
tags along with the original text that was between them.
For example if the HTML is:
This is <span class='add'>span 1</span> and this is <span class='add'>span 2</span>.
then $a
would be a list of length 4: {<span class='add'>span 1</span>} {span 1} {<span class='add'>span 2</span>} {span 2}
.
The sample code generates:
This is <em>span 1</em> and this is <em>span 2</em>.
as expected; but it seems that this must be an inefficient way to do this and, somehow, the capturing group should be usable directly within the regsub
expression.
Is this true and how is it done?
Something like:
set html [regsub "<span class='add'>(. ?)</span>" $html "<em>.../em>"]
where the ...
is something that points to the captured group.
Thank you.
set a [regexp -all -inline -- {<span class='add'>(. ?)</span>} $html]
if { [llength $a] > 0 } {
foreach {x y} $a {
set html [regsub "<span class='add'>${y}</span>" $html "<em>${y}</em>"]
}
}
CodePudding user response:
You can use a backreference in the replacement:
regsub -all {<span class='add'>(. ?)</span>} $html {<em>\1</em>}