我将一些行从一个表移到另一个表,我需要在操作期间修改内容。匹配选择应仅包含索引(数字)。它通常在开头,但我添加了这个类,以确保匹配整行中的好类。
$linehtml.replace('/<td class="index">[0-9]+</td>/', '<td>foo</td>');
我尝试过:
'/<td class="index">[0-9]+</td>/'
'<td class="index">/[0-9]+/</td>'
根据http://regexpal.com/我应该没问题,但jquery正则表达式看起来有些不对。
以下是一个例子:
<td class="index">5</td> <td>11</td> <td>History</td> <td id="description_5">here is the description</td>
最后看起来应该是这样:
<td>foo</td> <td>11</td> <td>History</td> <td id="description_5">here is the description</td>
编辑:
我的目标是从第二张表格移动到第一张表格:http://jsfiddle.net/k53Pz/2/
单击“单击以添加”单元格(抱歉,它是法语,我创建了快速小提琴,因为我刚开始这个工作流程。也许这不是一个好方法)
答案 0 :(得分:1)
如果您正在使用jQuery,为什么要使用正则表达式?
$('.index').html('foo').removeAttr('class');
答案 1 :(得分:1)
JavaScript支持正则表达式作为文字,因此不要引用它们。 e.g。
s.replace(/<td class="index">[0-9]+<\/td>/, '<td>foo</td>');
请记住,你需要逃避正斜杠等等。
通过引用它们,你会说它将它视为普通字符串,而不是正则表达式。