• JS

JS로 영어를 한글로 변환

2018-12-13 155

설명

  • 카카오톡 링크

JS로 영어를 입력하고 버튼을 눌렀을때 콘솔에 한글이 나타나는 소스입니다.
검색할때 사용하시면 유용할 것 같습니다~!

소스실행 현황

소스보기( X표시가 나타나면 무시하셔도 됩니다! )

<script type="text/javascript">
	var convertEngToKor = function(args) {
	var engChosung = "rRseEfaqQtTdwWczxvg"
	var engChosungReg = "[" + engChosung + "]";
	var engJungsung = {k:0,o:1,i:2,O:3,j:4,p:5,u:6,P:7,h:8,hk:9,ho:10,hl:11,y:12,n:13,nj:14,np:15,nl:16,b:17,m:18,ml:19,l:20};
	var engJungsungReg = "hk|ho|hl|nj|np|nl|ml|k|o|i|O|j|p|u|P|h|y|n|b|m|l";
	var engJongsung = {"":0,r:1,R:2,rt:3,s:4,sw:5,sg:6,e:7,f:8,fr:9,fa:10,fq:11,ft:12,fx:13,fv:14,fg:15,a:16,q:17,qt:18,t:19,T:20,d:21,w:22,c:23,z:24,x:25,v:26,g:27};
	var engJongsungReg = "rt|sw|sg|fr|fa|fq|ft|fx|fv|fg|qt|r|R|s|e|f|a|q|t|T|d|w|c|z|x|v|g|";
	var regExp = new RegExp("("+engChosungReg+")("+engJungsungReg+")(("+engJongsungReg+")(?=("+engChosungReg+")("+engJungsungReg+"))|("+engJongsungReg+"))","g");
	var converter = function (args, cho, jung, jong) {
		return String.fromCharCode(engChosung.indexOf(cho) * 588 + engJungsung[jung] * 28 + engJongsung[jong] + 44032);
	};
	var result = args.replace(regExp, converter);
	console.log(result);
	return result;
}

function run(engStr) {
	convertEngToKor(engStr);	
}

var convertEngToKor2 = (function () {
	var engChosung = "rRseEfaqQtTdwWczxvg"
	var engChosungReg = "[" + engChosung + "]";
	var engJungsung = {k:0,o:1,i:2,O:3,j:4,p:5,u:6,P:7,h:8,hk:9,ho:10,hl:11,y:12,n:13,nj:14,np:15,nl:16,b:17,m:18,ml:19,l:20};
	var engJungsungReg = "hk|ho|hl|nj|np|nl|ml|k|o|i|O|j|p|u|P|h|y|n|b|m|l";
	var engJongsung = {"":0,r:1,R:2,rt:3,s:4,sw:5,sg:6,e:7,f:8,fr:9,fa:10,fq:11,ft:12,fx:13,fv:14,fg:15,a:16,q:17,qt:18,t:19,T:20,d:21,w:22,c:23,z:24,x:25,v:26,g:27};
	var engJongsungReg = "rt|sw|sg|fr|fa|fq|ft|fx|fv|fg|qt|r|R|s|e|f|a|q|t|T|d|w|c|z|x|v|g|";
	var regExp = new RegExp("("+engChosungReg+")("+engJungsungReg+")(("+engJongsungReg+")(?=("+engChosungReg+")("+engJungsungReg+"))|("+engJongsungReg+"))","g");
	var converter = function (args, cho, jung, jong) {
		return String.fromCharCode(engChosung.indexOf(cho) * 588 + engJungsung[jung] * 28 + engJongsung[jong] + 44032);
	};
	return (function (args) {
		var result = args.replace(regExp, converter); 
		console.log(result);
		return result; 
	});
})();

function run2(engStr) {
	convertEngToKor2(engStr);
}
</script>

<input id="eng" value="wlsgud vhxmvhffldh">
<button onclick="run(document.getElementById('eng').value)">run</button>
<button onclick="run2(document.getElementById('eng').value)">run2</button>