So this is my code:
function PlusMinus() {
str = str.replace(/([^\d\.](?!.*[^\d\.]))/, function(m, $1) {
if ($1 == "-") return "";
return $1 + "-";
});
}
And it isn't working.
What I want to do, is to find the last occurrence of any character that is not a number (0-9) or a dot (.) and then if it is a "-", remove it, if it isn't replace the char with the char + "-".
Simply like a calculators -/+ button.
Examples:
"" -> "-"
"-" -> ""
"abc2-3" -> "abc23"
"a2-" -> "a2"
"s23.3" -> "s-23.3"
"4f" -> "4f-"
And yes, I've seen this: JavaScript RegExp: Can I get the last matched index or search backwards/RightToLeft? but I can't get it to work for chars in brackets
.replace()returns the new string instead of modifying the input one? That is to say, you need a left var:anyVar = str.replace(...);.