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
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.