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,
I think your regexes are ok:

<https://regex101.com/r/j0MXV5/1>

<https://regex101.com/r/oN9v8U/1>

Maybe it is an option to use [blur](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/blur)  and trim the either the leading and trailing whitespaces:

`onblur="this.value=this.value.trim();`

Or only the trailing whitespaces:

`onblur="this.value=this.value.replace(/\s+$/, '');`

```html
<form>
    <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}[^\\S]+$" onblur="this.value=this.value.replace(/\s+$/, '');">
</form>
```