Question: I am trying to accept an email without having whitespace or blank at the end.
I have tried two variants

> 
> pattern="^[A-Za-z0-9.\_%+-]+@[a-z0-9.-]+.[a-z]{1,4}[^\s]+$">
> 
> 
> pattern="^[A-Za-z0-9.\_%+-]+@[a-z0-9.-]+.[a-z]{1,4}\S$">
> 
> 
> 

both are not working & input is accepting whitespace at the end. My full input line is 

> 
> input type="email" id="guestUserEmail" name="guestEmail" data-pattern-error="Email is invalid" data-required-error="Please enter email address" required pattern="^[A-Za-z0-9.\_%+-]+@[a-z0-9.-]+.[a-z]{1,4}$"
> 
> 
> 

I referred this answer [regex for no whitespace at the begining and at the end but allow in the middle](https://stackoverflow.com/questions/34974942/regex-for-no-whitespace-at-the-begining-and-at-the-end-but-allow-in-the-middle)

Please suggest!

Answer: 
Java escape sequence will not consider `\`, you have to append one more slash, like this `\\`. 

Modify your String pattern, like this

```
^[A-Za-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{1,4}[^\\S]+$

```

This will resolve,
You are using a HTML `input` field of type `email`. The [defined and documented behavior](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email) of this element is to *accept but remove* any leading or trailing whitespace. If that's not acceptable, don't use this element.

Your attempt at "fixing" the regex are misdirected. Basic regex reading skills should tell you that the apparent original regex only allows alphabetics just before end of string. Your attempts would basically change it from "mostly nearly correct email" to "mostly nearly correct email, followed by any junk at all, as long as it's not whitespace".

Generally speaking, you fix a too-permissive regex by constraining it more (dropping some stuff it would previously accept, perhaps refactoring the regex along the way; for example, to only allow internal dashes in a domain part, you have to split it into first character, optional middle perhaps with dashes, slightly less optional last character without a dash) but certainly not by adding new matching possibilities -- especially not a broad generic character class repeated an arbitrary number of times.

As already noted in comments, there is no way to completely match every valid email address with a regex, but the regular built-in validation in HTML5 is pretty much guaranteed to do a better job than a random regex you found on some PHP forum.