0

如何使用正则表达式删除 HTML 标记 ID 属性中的多个“[”?

例如

<div id="test[12][45][67]">test inline [for example]</div>

改成

<div id="test12]45]67]">test inline [for example]</div>
4

2 回答 2

2

What you could (most likely should) do is to use a PHP HTML parser, such as PHP Simple DOM Parser to go over the HTML, extract the ID value and then replace it using something like so:

<?php
$string = <yourID>
$pattern = '/\[/g';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>

This would allow you to safely modify only your id section in a relatively simple manner.

于 2012-07-05T08:48:01.527 回答
0

使用 javascript:

function rem_open_square_brackets(_id) {
    if(_id) {
        return _id.replace(/[\[]*/g, '');
    }
    return _id;
}
var _id = rem_open_square_brackets('test[12][45][67]');
document.write(_id);

您可以找到所有 id 并将它们传递给函数并用返回值替换它们。

于 2012-07-05T09:08:28.150 回答