regular expression for username code example

Example 1: regex for username

let username = '';
username = username.replace(/\s/g,'_');
username = username.replace(/\-/g,'.');
username = username.match(/[a-zA-Z0-9\.\s]+/g).join('_');

Example 2: username regex

^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$

Example 3: username validation

<html>
<head>
	<title></title>
</head>
<body>
<form method="post" action="">
	username:<input type="text"  id="name" onkeyup="validation()">
	</form>
</body>
<script type="text/javascript">
	function validation(){
	var username=document.getElementById("name").value;///get id with value 
	var usernamepattern=/^[A-Za-z .]{3,15}$/;////Regular expression
	if(usernamepattern.test(username))
	{
		document.getElementById("name").style.backgroundColor='yellow';
    }
    else
    {
    	document.getElementById("name").style.backgroundColor='red'; }
	}

</script>
</html>