By GokiSoft.com| 19:34 30/10/2021|
Tài Liệu Javascript

[Ứng Dụng] Remove Vietnamese from String by Javascript

#remove_vietnamese.html


<!DOCTYPE html>
<html>
<head>
	<title>Remove Vietnamese from String by Javascript</title>
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Remove Vietnamese from String by Javascript</h2>
			</div>
			<div class="panel-body">
				<textarea class="form-control" id="original" rows="5" placeholder="Nhập dữ liệu cần xử lý"></textarea>
				<input type="text" id="character" class="form-control" placeholder="Chèn ký tự thay thế dấu cách" style="margin-top: 20px;">
				<textarea class="form-control" id="result" rows="5" style="margin-top: 20px; margin-bottom: 20px" placeholder="Kết quả"></textarea>
				<button class="btn btn-warning" onclick="removeAll()">Remove All</button>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		function removeAll() {
			str = $('#original').val().trim()
			str = remove_unicode(str, $('#character').val(), true)
			$('#result').val(str)
		}

		function remove_unicode(str, c = '-', isKeepCase = false) 
		{  
		   if(!isKeepCase) {
		   		str= str.toLowerCase();
		   }
		   str= str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");  
		   str= str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");  
		   str= str.replace(/ì|í|ị|ỉ|ĩ/g,"i");  
		   str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");  
		   str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");  
		   str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");  
		   str= str.replace(/đ/g,"d");

		   str= str.replace(/À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ/g,"A");  
		   str= str.replace(/È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ/g,"E");  
		   str= str.replace(/Ì|Í|Ị|Ỉ|Ĩ/g,"I");  
		   str= str.replace(/Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ/g,"O");  
		   str= str.replace(/Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ/g,"U");  
		   str= str.replace(/Ỳ|Ý|Ỵ|Ỷ|Ỹ/g,"Y");  
		   str= str.replace(/Đ/g,"D");

		   str= str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g,c); 
		 
		   str= str.replace(`/${c}+${c}/g`,c);
		   str= str.replace(`/^${c}+|${c}+$/g`,"");  
		 
		   return str;  
		} 
	</script>
</body>
</html>


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)