Reputation: 652
I have a value like this
<option value="RB+TE+WR+DEF+CB">text</option>
I'm trying to replace all instances of "+" with a "-"
Both my tries only replaced the 1st "+" and results were
<option value="RB-TE+WR+DEF+CB">text</option>
This is what i've tried
$('option').each(function () {
$(this).val(function (i, v) {
return v.replace("+", "-");
});
})
$('option').val(function (i, v) {
return v.replace("+", "-");
});
$('option').val(function (i, v) {
return v.replace(new RegExp("/+/", "g"), '-');
});
I need the result to be
<option value="RB-TE-WR-DEF-CB">text</option>
Upvotes: 0
Views: 29
Reputation: 338316
You need to escape the +
in regular expressions.
$('option').val(function (i, v) {
return v.replace(/\+/g, "-");
});
or use .replaceAll()
(does not work in IE)
$('option').val((i, v) => v.replaceAll("+", "-"));
Upvotes: 1