Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
How to exclude last space from regex?

I have input box in my react component

const {Component} = React;

class Example extends Component {
    constructor(props) {
        super(props);
        this.state = {
            searchText: "",
        };
    }

    restrictSpecialCharacters = (e) => {
        const nospecial = /^[A-Za-z0-9]+$/;
        const textValue = e.target.value;
        if (textValue.match(nospecial)) {
            this.setState({ searchText: textValue })
        }
    };

    render() {
        return <input
            type="text"
            className="form-control form-control-lg material-textfield-input"
            name="carrierACNumber"
            value={this.state.searchText}
            onChange={(e) => this.restrictSpecialCharacters(e) }
            required
        />;
    }
}

ReactDOM.render(<Example/>, document.getElementById("root"));
<div id="root"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>

it works as expected but when I try to remove last character on pressing backspace then it does not remove it. How can I make regex that removes that last character?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The reason it's not allowing you to remove the last character is because the + in your regex means "one or more" which does not allow for 0 characters to appear; it no longer matches the regex.

If you change your regex to /^[A-Za-z0-9]*$/ that should resolve your problem, as * is 0 or more:

const {Component} = React;

class Example extends Component {
    constructor(props) {
        super(props);
        this.state = {
            searchText: "",
        };
    }

    restrictSpecialCharacters = (e) => {
        const nospecial = /^[A-Za-z0-9]*$/;
        const textValue = e.target.value;
        if (textValue.match(nospecial)) {
            this.setState({ searchText: textValue })
        }
    };

    render() {
        return <input
            type="text"
            className="form-control form-control-lg material-textfield-input"
            name="carrierACNumber"
            value={this.state.searchText}
            onChange={(e) => this.restrictSpecialCharacters(e) }
            required
        />;
    }
}

ReactDOM.render(<Example/>, document.getElementById("root"));
<div id="root"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!