how to write below regex without catastrophic backtracking

I’m trying to use a regex in javascript where special characters and leading and trailing spaces are not allowed in an input element. Maximum character length is 50.

The regular expression I’m using is

/^[a-zA-Z0-9]+([ ]?[a-zA-Z0-9]+)*$/

But this is causing issues when the string is large and there is a special character in the end. On research I found that this is happening due to catastrophic backtracking but I’m unable to optimise the regex. Kindly help me find an optimum solution to this issue.

I tried debouncing the keyup event for the input element but that is also not solving the issue.