How to check the last two elements in the input string for an operation sign? It is necessary to separate the number in the line from the sign and if there are two signs (+ -), then write the last (-). As I understand it, you just need to add conditions in if :

 function changeAction(value) { //var input = document.getElementById("t"); var onlyNumbers = /[\/*+-]{2}/; //пееревірка чи введені не цифри if (onlyNumbers.test(calculator.answer.value)) { alert("lol2"); if (calculator.answer.value =='++' || calculator.answer.value == '-+' || calculator.answer.value =='/+' || calculator.answer.value =='*+') { calculator.answer.value = (calculator.answer.value.substr(0,calculator.answer.value.length -2)); //alert(calculator.answer.value.substr(0,calculator.answer.value.length-2)); } if (calculator.answer.value =='--' || calculator.answer.value == '/-' || calculator.answer.value =='*-' || calculator.answer.value =='+-') { calculator.answer.value = '-'; } if (calculator.answer.value =='-*' || calculator.answer.value == '/*' || calculator.answer.value =='**' || calculator.answer.value =='+*') { calculator.answer.value = '*'; } if (calculator.answer.value =='//' || calculator.answer.value == '*/' || calculator.answer.value =='-/' || calculator.answer.value =='+/') { calculator.answer.value = '/'; } } } 

    1 answer 1

     function handler(){ let text = document.querySelector('#test'), reg; // Ищем в конце знаки +, -, *, /, сколько бы их не было // Проверку на то, является ли строка числом с знаками, можно, при желании, вставить потом reg = /^([^\-\+*\/]*)([\-\+*\/]+)$/.exec(text.value.trim()); // Если они есть, вставляем содержимое ДО знаков и самый последний из введённых // some+-+--+ -> some+ // some+-+ -> some+ // some+--+- -> some- if(reg !== null){ text.value = reg[1] + reg[2][reg[2].length - 1]; } } document.addEventListener('DOMContentLoaded', e => { document.querySelector('#check').addEventListener('click', handler); }); 
     <input type='text' id='test' /><br /> <input type='button' id='check' value='Проверить' /> 

    • Thank you .. works + -, but * and / does not work, what's the matter? - Archi
    • @Archi, you can add other characters to the regular schedule. There is only a plus and a minus, and a bunch of signs themselves: ru.wikipedia.org/wiki/… Choose any and add :) - user207618