'How to remove duplicate arithmetic operators using regex

this is my input string in react : 4-+3-4 I want to check onChange() function when a duplicate arithmetic entered, like this -+ replace last operator with first one, how can i detect that on change function? input should change on change to 4+3-4 I tried and tested but failed This is my regex for finding operators and last operator /[+*\/-]/g /[+*\/-]$/g



Solution 1:[1]

You may try a regex replace approach:

var input = "4-+3-4";
var output = input.replace(/[*/+-]+([*/+-])/g, "$1");
console.log(output);

This approaches matches two or more successive arithmetic operators, capturing the final one. It then replaces with just this final operator.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Biegeleisen