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
CodePudding user response:
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.
CodePudding user response:
let input1 = "4- 3-4";
let input2 = "4 3-4"
let output1 = input1.replace(/[ *\/-] ([ *\/-])/g, "$1");
let output2 = input2.replace(/[ *\/-] ([ *\/-])/g, "$1");
console.log(output1);
console.log(output2);