0

Is it possible to find all text inside a node and take the matched text and replace the contents of a node using only a single regular expression? I should say that this is in a file.

Given:

<x>This text</x>
<!-- Unknown number of nodes between <x> and <y> -->
<y>Junk</y>

Change to:

<x>This text</x>
<!-- Unknown number of nodes between <x> and <y> -->
<y>This text</y>

Normally, I would do a regular expression to find the contents of x and store it in a variable. Then, I would run a second regular expression to find the contents of y and replace it with the variable's data. Just wondering if there is a "1-step" solution... Thanks.

Stephen
  • 2,410
  • 3
  • 33
  • 57
  • 3
    [Do not parse XML with regular expressions!](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Ether Aug 26 '10 at 19:50

4 Answers4

3

If you use JQuery, you could simply do this:

$('y').html($('x').html());

Otherwise, with standard JavaScript:

document.getElementsByTagName('y')[0].innerHTML = document.getElementsByTagName('x')[0].innerHTML;
js1568
  • 7,012
  • 2
  • 27
  • 47
2
$filecontents =~ s!(<x>(?>(.*?)</x>)(?>.*?<y>))(?>.*?(</y>))!$1$2$3!s;

But you are better off using an XML parser (assuming this is XML). For instance, the above won't work with your sample text, because it will think the <y> in the comment is the beginning of the y tag.

ysth
  • 96,171
  • 6
  • 121
  • 214
0

If you have a string that you want to replace, this seems to work.

<script>
    var text = "<x>This text<x>\n<y>Junk<y>";
    var replaced = text.replace(/<x>(.*)<x>\n<y>.*<y>/s, "<x>$1</x>\n<y>$1</y>");
    alert(replaced);
</script>
pauloya
  • 2,535
  • 3
  • 30
  • 50
  • The dot loses its special meaning in character classes, so `[.\n]` matches a dot or a newline; you probably want another `.*` there. And you should use the `s` modifier, not `m`. Then the regex should work, but with all those greedy `.*`s, you might find it unacceptably slow. (That's assuming `` and `` are unique, but the OP says they are.) – Alan Moore Aug 26 '10 at 18:24
  • @Alan Thanks for the correction. I edited the code, but I guess the question is not about javascript anymore.. – pauloya Aug 27 '10 at 07:31
0

I was able to contact a friend about this.

The regEx, assuming only one x and one y node, would look like this.

s/X([^X]*)X([^Y]*)Y([^Y]*)Y/X\1X\2Y\1Y/

where X is <x> and Y is <y>

Stephen
  • 2,410
  • 3
  • 33
  • 57