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: 
Update
======

HTML by default collapses whitespace. This means:

* If there are more than one whitespace between chars, it will render as a single whitespace char.

	+ ex.`this string has a double space right here .`   
	will render as:  

	`this string has a double space right here .`
* Leading and trailing whitespace are stripped (this doesn't happen with strings hence the necessity for methods such as `trim()`)

	+ ex. `abc123@email.com&nbsp;&nbsp;&nbsp;`   
	will render as:  
	`abc123@email.com`

So if you have a billion spaces after the email address, it will be automatically stripped in an `<input>`. The Demo has your `<input>` wrapped in a `<form>` and the form will actually send data to a real test server. If you send a valid email address with trailing whitespace, look at the response. You'll see that in the response, that the value has no trailing whitespace.

---

Have you considered escaping the periods? 

A `.` means **ANY** one char

A `\.` means a dot or period

It looks like this works:

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

```

See tests at [RegEx101](https://regex101.com/r/N967YZ/1/tests)

---

Demo
----

```css
input {
  font: inherit
}
```

```html
<form id='contact' action='https://httpbin.org/post' method='post'>
  <input type="email" id="guestEmail" name="guestEmail" pattern="^[A-Za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{1,4}$" required placeholder='abc123@email.com'>
  <input type='submit'>
</form>
```
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,