In Java Script, regular expressions are also objects.
These patterns are used with the exec and test methods.
Also we will use trim() function that will trim leading whitespace off the string.
This wont be perfect validation it is possible to slip not compliant addresses by it but it's normally good enough.
You can use them to constrain input, apply formatting rules, and check lengths.Next, we use the Java Script regular expression /\W/ to forbid illegal characters from appearing in usernames.We want to allow only letters, numbers and underscopes.At first we check if the user entered anything at all in the email field.Next, we use regular expression and the test() method to check the field for a compliance.
For that we use the seacrh() method and two more regular expressions: /(a-z) / and /(0-9)/.