그거/Tech
JavaScript String replace
뽀기
2009. 7. 9. 20:56
오랜만에 JavaScript를 했더니.. 머리 쥐난다. -_-;
예전에 표준을 지키지 않고 코딩(?)하던 이후로는 거의 처음하는것 같은데..
좀 헤맸다는. ㅎㅎ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>TEST</title>
<script language="JavaScript">
<!--
function gogo() {
var str="<font size='3'>This is test</font>"
// str에서 < 를 모두 찾아서 < 로 치환
var str2=str.replace(/\</g, "<");
// str에서 > 를 모두 찾아서 > 로 치환
var str2=str2.replace(/\>/g, ">");
document.getElementById("str").value=str;
document.getElementById("content").value=str2;
}
//-->
</script>
</head>
<body onLoad="javascript:gogo()">
<textarea cols=50 rows=5 id="str"></textarea>
<br>
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
<form name="frm">
<textarea cols=50 rows=5 id="content"></textarea>
</form>
</body>
</html>
<html>
<head><title>TEST</title>
<script language="JavaScript">
<!--
function gogo() {
var str="<font size='3'>This is test</font>"
// str에서 < 를 모두 찾아서 < 로 치환
var str2=str.replace(/\</g, "<");
// str에서 > 를 모두 찾아서 > 로 치환
var str2=str2.replace(/\>/g, ">");
document.getElementById("str").value=str;
document.getElementById("content").value=str2;
}
//-->
</script>
</head>
<body onLoad="javascript:gogo()">
<textarea cols=50 rows=5 id="str"></textarea>
<br>
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
<form name="frm">
<textarea cols=50 rows=5 id="content"></textarea>
</form>
</body>
</html>
# 결과