Kian Abrishami zand
Kian Abrishami zand

Reputation: 13

How to solve script's Invalid regular expression flags?

How can I solve this code error when running!

var replaced = $("body").html().replace(/<td>YES</td>/g,'<span class="yes"></span>');
$("body").html(replaced);

I want to replace all

<td>YES</td>

with

<span class="yes"></span>

Upvotes: 0

Views: 150

Answers (2)

user6612486
user6612486

Reputation: 1

try to use an escape character for the closing td in your expression

var replaced = $("body").html().replace(/<td>YES<\/td>/g,'<span class="yes"></span>');
$("body").html(replaced);

Upvotes: 0

Dan D.
Dan D.

Reputation: 74645

Consider:

$("td").filter(function() {
    return $(this).text() === 'YES';
}).replaceWith('<span class="yes"></span>');

This finds all the td elements and filters for the ones with YES as the only contents and the replaces them.

Upvotes: 1

Related Questions