0

I need to improve the following Regex to add two conditions: it should not allow "00" (or any number of leading zeros) and neither should it allow "1..." (only one decimal after a whole number). For instance, an user can click "0" twice, but only one "0" would be allowed. The same with the operators: an user can click "+-" (or any combination thereof) but only "-" would be allowed. Same with "..", etc. This should not apply to numbers (1233388479223 can be allowed).

this.setState(prevState => ({
  value: `${prevState.value}${result}`.replace(/([\/\+-/*=])([\/\+\-\*=])/gi, '$2')
}))

Calculator code

class Calculator extends Component {
  constructor(props) {
    super(props);
    this.state = {value:""}
  this.handleClick = this.handleClick.bind(this);
      }
  handleClick(evt){
 const id=evt.target.id;
 const result= evt.target.value;

this.setState(prevState => ({
  value: `${prevState.value}${result}`.replace(/([\/\+-/*=])([\/\+\-\*=])/gi, '$2')
}));

if(id==="equals"){
    this.setState({value: math.eval(this.state.value)})
}
else if(id==="clear"){
this.setState({value : 0})  
 }

}



render() {
    return(
            <div id="container">
                <Display value={this.state.value} />
                <Button onClick={this.handleClick} id="zero" value={'0'} />
                <Button onClick={this.handleClick} id="one" value={'1'} />
                <Button onClick={this.handleClick} id="two" value={'2'}/>
                <Button onClick={this.handleClick} id="three" value={'3'} />
                <Button onClick={this.handleClick} id="four" value={'4'} />
                <Button onClick={this.handleClick} id="five" value={'5'} />
                <Button onClick={this.handleClick} id="six" value={'6'} />
                <Button onClick={this.handleClick} id="seven" value={'7'} />
                <Button onClick={this.handleClick} id="eight" value={'8'}  />
                <Button onClick={this.handleClick} id="nine" value={'9'} />
                <Button onClick={this.handleClick} id="decimal" value={'.'} />
                <Button onClick={this.handleClick} id="equals" value={'='} />
                <Button onClick={this.handleClick} id="clear" value={'clear'}  />
                <Button onClick={this.handleClick} id="add" value={'+'} />
                <Button onClick={this.handleClick} id="subtract" value={'-'} />
                <Button onClick={this.handleClick} id="multiply" value={'*'} />
                <Button onClick={this.handleClick} id="divide" value={'/'} />
            </div>
)

}
Hernan Ariel
  • 263
  • 3
  • 9
  • Within square brackets, [you don't need to escape most special characters](https://stackoverflow.com/questions/19976018/does-a-dot-have-to-be-escaped-in-a-character-class-square-brackets-of-a-regula). – Tim Oct 24 '18 at 16:20
  • What do you mean by "should not allow"? What would be the expected output when the input is illegal? – Håken Lid Oct 24 '18 at 16:26
  • ".." should become ".", for instance, and "00" should become "0". Also, 0 should be replaced with the next number you enter if it´s the first one (01 becomes 1). – Hernan Ariel Oct 24 '18 at 16:28
  • so, what would the allowed values be? – c-chavez Oct 24 '18 at 18:35
  • I´ll edit the question with the Calculator code, so that it makes more sense. Sorry for being unclear. – Hernan Ariel Oct 24 '18 at 18:40

1 Answers1

0

You will need several regexes to do what you want. You can call them one by one.

First, there's an error in your regex, you have not escaped the minus char (indicates a range of chars), it should be:

/([\/+\-/*=])([\/+\-*=])/g

Remove leading zeros, if followed by a digit:

   /^0+(?=[1-9])/

Replace with an empty string.


Remove all but one leading zero, when followed by a dot:

/^0+(?=\.)/

Replace with a zero '0'.


Remove multiple dots:

/\.+/g

Replace with a dot.

You should call replacements one by one. Hope this helps you.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57