如何将输入值更改为下标?

时间:2019-04-24 18:20:28

标签: reactjs

我希望我的handleChange函数将输入的值更改为下标。在React中怎么做?

class App extends Component {
  state = {
    equation: ""
  };

  handleChange = event => {
    this.setState({ [event.target.name]: event.target.value });
  };

  render() {
    const { equation } = this.state;
    console.log(equation);
    return (
      <div>
        <input onChange={this.handleChange} value={equation} name="equation" />
      </div>
    );
  }
}

1 个答案:

答案 0 :(得分:2)

正则表达式选择+捕获数字字符,并替换为相应的下标html实体:

  handleChange = event => {
    const formattedValue = event.target.value.replace(/(\d)/g, '&#832$1;');
    this.setState({ [event.target.name]: formattedValue });
  };