简体   繁体   English

JavaScript正则表达式 - 在填充的表格单元格后删除空表格单元格

[英]JavaScript regex - remove empty table cell after a populated table cell

I've been banging my head against this one for a week, just can't get it to work, please can you help? 我已经打了一个星期对着这个一个星期,只是不能让它工作,请你帮忙吗? I process user input, transforming it into a HTML table. 我处理用户输入,将其转换为HTML表格。 For each empty cell, I create a cell that contains 1 vertical tab character which I then use to (try to) do the following: I need to remove 1 table cell (that contains 1 vertical tab character) only if it appears after a table cell that contains 0 or more non-vertical-tab characters. 对于每个空单元格,我创建一个包含1个垂直制表符的单元格,然后我将其用于(尝试)执行以下操作:我需要删除1个表格单元格(包含1个垂直制表符),只有当它出现在表格后面时包含0个或更多非垂直制表符的单元格。

For example... 例如...

<table>
<tr>
<td>1</td>
<td></td> // this cell should be removed
<td></td>
<td></td>
<td></td>
<td>some text</td>
<td></td> // this cell should be removed
<td></td>
<td></td>
<td></td>
<td>4973as</td>
<td></td> // this cell should be removed
<td></td>
<td></td>
<td></td>
<td>90-16</td>
<td>sdf;.s'df'f</td>
<td>£%$Dcgcfcgf</td>
<td></td> // this cell should be removed
<td></td>
<td></td>
</tr>
</table>

I'm trying to do this in JavaScript and a lookbehind seemed to be the answer, but JavaScript's regex engine doesn't support lookbehinds. 我试图用JavaScript做这件事,看起来好像是答案,但JavaScript的正则表达式引擎不支持lookbehinds。 So I tried this in PHP which worked perfectly (in regex101.com) with the data in $testInput... 所以我在PHP中尝试了这一点,它在$ testInput中完美地工作(在regex101.com中)...

/(?<=<td>[^\x0B]*<\/td>)<td>\x0B<\/td>/g

...until I realised PHP doesn't allow the global modifier. ...直到我意识到PHP不允许全局修饰符。 I tried working around this by using preg_replace_all() - which doesn't exist, but preg_match_all() does, which you can use in a loop to execute preg_replace(). 我尝试使用preg_replace_all()来解决这个问题 - 它不存在,但preg_match_all()会这样做,你可以在循环中使用它来执行preg_replace()。 Problem with this? 问题呢? Warning: preg_match(): Compilation failed: lookbehind assertion is not fixed length at offset 19 警告:preg_match():编译失败:lookbehind断言在偏移量19处不是固定长度

$testInput = "<table id=\"testInput\"><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>4</td><td></td><td>4</td><td></td><td>4</td><td></td><td>4</td><td></td><td>4</td><td></td><td>4</td><td></td><td>4</td><td></td><td>4</td><td></td></tr><tr><td></td><td>4</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td>4</td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td>4</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td>4</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td>4</td><td></td><td></td><td></td><td>4</td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></table>";

$pattern = "/(?<=<td>[^\x0B]*<\/td>)<td>\x0B<\/td>/";

    pregReplaceAll($pattern, "", $testInput);

function pregReplaceAll($find, $replacement, $s) {
    while (preg_match($find, $s)) {
        $s = preg_replace($find, $replacement, $s);
    }
    echo $s;
}

The above script results in an echo of the input with no replace having occurred. 上面的脚本导致输入的回显没有发生替换。

I've been plugging away at this daily and my brain's gone numb... it seems so simple and yet so complicated. 我每天都在忙着这个,而且我的大脑已经麻木了...它看起来如此简单而又如此复杂。

My last resort is find a solution in C#/ASP but that's another new thing to learn... not that that's a bad thing, but JavaScript/PHP can't be this crippled in 2017 can it?? 我的最后一招是在C#/ ASP中找到一个解决方案,但这是另一个要学习的新东西......不是那件坏事,但JavaScript / PHP不能在2017年瘫痪吗?

preg_replace does replace all occurrences, so you should stick with that. preg_replace 替换所有出现的内容,因此您应该坚持使用它。 Indeed, a look behind cannot have a * in it, so that wont work. 事实上,背后的外观不能有* ,所以不会工作。 But you can use a capture group for the part you want to keep, and inject it again with $1 : 但是您可以使用捕获组来保留要保留的部分,并使用$1再次注入它:

$pattern = "/(<td>[^\x0B]*<\/td>)<td>\x0B<\/td>/";
$result = preg_replace($pattern, "$1", $testInput);
echo $result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM