Polopollo
Polopollo

Reputation: 377

jquery regex can't match my numerical value

I move some rows from a table to another, and I need to modify the content during the operation. The matching selection should only contains an index (numerical). It is usually at the beginning, but I added the class to be sure to match the good one in the whole row.

$linehtml.replace('/<td class="index">[0-9]+</td>/', '<td>foo</td>');

I tried with:

'/<td class="index">[0-9]+</td>/'

'<td class="index">/[0-9]+/</td>'

I should be ok according to http://regexpal.com/, but something looks wrong with the jquery regex.

Here is an example:

<td class="index">5</td> <td>11</td> <td>History</td> <td id="description_5">here is the description</td>

It should looks like that at the end:

<td>foo</td> <td>11</td> <td>History</td> <td id="description_5">here is the description</td>

Edit:

My aim is to move a row from a second table to a first one: http://jsfiddle.net/k53Pz/2/

Click on the "Click to add" cell (sorry it is in French, I created the Fiddle fast because I just started this workflow. Maybe it isn't a good method to do it)

Upvotes: 0

Views: 143

Answers (2)

user719958
user719958

Reputation:

JavaScript supports regexes as literals, so don't quote them. e.g.

s.replace(/<td class="index">[0-9]+<\/td>/, '<td>foo</td>');

Keeping in mind that you need to escape your forward slashes and whatnot.

By quoting them, you're saying to treat it as a normal string, not a regular expression.

Upvotes: 1

Jay Blanchard
Jay Blanchard

Reputation: 34426

If you're using jQuery why use regex at all?

$('.index').html('foo').removeAttr('class');

See http://jsfiddle.net/k53Pz/

Upvotes: 1

Related Questions