How to remove part of string with javascript regex without repeating delimiting strings
javascript, regex, string
Solution
You can capture tag in a `captured group` and use it later as back reference:
var repl = s.replace(/<(foo)>.*?<\/\1>/, '<$1></$1>');
//=> hello <foo></foo>
Note `\1` and `$1` are back references to the captured group #1.
Problem
I would like to remove an unknown substring when it occurs between two known substrings (`<foo>` and `</foo>`). For example, I'd like to convert: ``` hello <foo>remove me</foo> ``` to: ``` hello <foo></foo> ``` I can do it with: ``` s = ... s.replace(/<foo>.*?<\/foo>/, '<foo></foo>') ``` but I'd like to know if there's a way to do it without repeating the known substrings (`<foo>` and `</foo>`) in the regex and the replacement text.