0

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>
MShack
  • 820
  • 1
  • 12
  • 30

1 Answers1

1

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("+", "-"));
Tomalak
  • 322,446
  • 66
  • 504
  • 612