I have a huge amount of files with elements containing an id. I would like to copy the value of the id and create a new attribute with the value of the id:
<element id="some_unique.value">
...
</element>
What I’m trying to achieve:
<element id="some_unique.value" new_attribute="some_unique.value">
...
</element>
How can I do this using a regular expression? I’m looking for a regex that I can use in my IDE (Visual Studio Code) to quickly change multiple files.
2
Answers
Capture the part you want to reuse as a group and use a back reference to construct the result.
Search:
Replace:
As in my comment try matching:
and replace with
See: regex101
Explanation
Match:
id="..."
: match literal id and text in quotation([^"]+)
: that is not quotation marks. save this to group 1Replace:
$0
: keep original datanew_attribute="
: add literal ‘ new_attribute=’"
: open quotation$1
: insert group 1 (quoted id text) and"
: close quotation