JavaScript RegExp to Replace Between Text

I wanted to replace all text between two tags. It seems odd, but it resovled an issue I had.

Here is the text:

Hello<table>
<tr>
<td>Some Text</td>
</tr>
</table> World

Here is the JavaScript command to replace all text between the <table and /table> tags with nothing.

string = 'Hello<table>\n<tr>\n<td>Some Text</td>\n</tr>\n</table> World';
var rx = new RegExp("<table[\\d\\D]*?\/table>", "g");
string.replace(rx, "");

Would result in: Hello World

The reason this works is: [\d\D] matches all characters, including new lines and spaces. Followed by: *?

Leave a Reply