Sometimes, we want to write regular expression for not allowing spaces in the input field with JavaScript.
In this article, we’ll look at how to write regular expression for not allowing spaces in the input field with JavaScript.
How to write regular expression for not allowing spaces in the input field with JavaScript?
To write regular expression for not allowing spaces in the input field with JavaScript, we use the \S
pattern.
For instance, we write
const regExp = /^\S+$/;
to use the \S
pattern to match non-space characters.
+
lets us match a group of non-space characters.
^
is the marker for the start of a word.
And $
is the marker for the end of a word.
Conclusion
To write regular expression for not allowing spaces in the input field with JavaScript, we use the \S
pattern.