-2

I'm failing to find the correct regex to solve this adaptation. From


<td> foo </td>
<td> bar </td>
|
<td> two foo </td>
<td> two bar </td>
|

I want to get to

<tr>
  <td> foo </td>
  <td> bar </td>
</tr></tr>
  <td> two foo </td>
  <td> two bar </td>
</tr>

By means of identifying N repetitions of the regex <td>(.*)</td>.

Could I get some help?

Thanks!

#EDIT: I am generating the html code myself from a different format ( initial input was

| foo | bar | | twofoo | twobar |

and I'm generating it from such).

I am doing the conversion on JS, but for trying it out I'm using https://regex101.com/, if it is useful to know.

ggonmar
  • 760
  • 1
  • 7
  • 28
  • [You should not use regex to parse HTML](https://stackoverflow.com/a/1732454/5784924). Try an HTML parser instead. – Nicolas Jul 16 '20 at 12:54
  • Also, if you are not using code to parse HTML, but want to use "find and replace" in a text editor/IDE, please add details of which tool you use and the regex flavour it uses if you know it – Kaddath Jul 16 '20 at 12:57
  • If you just want to match a pattern repeated N times, I believe you're looking for the {} notation. Would this work: `(?:.*?\R){N}` where `N` is the number of repetitions (2 in your example) – Charlie Armstrong Jul 16 '20 at 13:19

1 Answers1

2

If I were you, starting from your original input, I'd treat each part (beginning of table / between tds / between trs / end of the table) separately. It's easier and you process each part once.

You can add a space or \s in the regexes depending if you want to keep the space around your words, I didn't to match your expected output.

In this order:

begin: ^\| -> replace with <tr>\n <td>

end: \|$ -> replace with </td>\n</tr>

between trs: \| \| -> replace with </td>\n</tr>\n<tr>\n <td>

finally, between tds: \| -> replace with </td>\n <td>

Exemple with js:

var input = '| foo | bar | | twofoo | twobar |';

input = input.replace(/^\|/, '<tr>\n  <td>');
input = input.replace(/\|$/, '</td>\n</tr>');
input = input.replace(/\| \|/g, '</td>\n</tr>\n<tr>\n  <td>');
input = input.replace(/\|/g, '</td>\n  <td>');

console.log(input);
Kaddath
  • 5,933
  • 1
  • 9
  • 23