[Day3] JS 3 [11/22]
--๋ณต์ต--
1. test01.html ํ์ด์ง์์ select ํ๊ทธ์ option์ ์ ํํ๋ฉด test01_ok.jsp ํ์ด์ง๋ก submit ๋์ด์ radio ๋ฒํผ์ด ์ฒดํฌ๋๋๋ก js๋ก ์ฝ๋ฉํ์ธ์~
[sample01.html]
-html-
<form action="sample01_ok.jsp" method="get">
<select id="selsubject" name="selsubject">
<option>์ ํํ์ธ์.</option>
<option value="kor">๊ตญ์ด</option>
<option value="eng">์์ด</option>
<option value="mat">์ํ</option>
<option value="pe">์ฒด์ก</option>
</select>
</form>
-(form ํ๊ทธ ์์ด์ผ ์ ๋ ฅํด์ get, post ๊ฐ๋ฅ!)
-js-
// 1. selsubject ์ option์ ์ ํํ์ ๋ ์๋ธ๋ฐ(submit)
// 2. ๋ชจ๋ ์
๋ ฅํ๊ทธ๋ค์ form ํ๊ทธ ์์ ์์ด์ผ ํ๋ค.
// 3. ?ํ๋ผ๋ฏธํฐ์ด๋ฆ=๊ฐ&ํ๋ผ๋ฏธํฐ์ด๋ฆ=๊ฐ X
document.getElementById("selsubject").onchange = function (){
var svalue = this.options[this.selectedIndex].value ;
if(svalue == "์ ํํ์ธ์."){
alert("๊ณผ๋ชฉ๋ง ์ ํํ์ธ์.");
return;
}
// js ์๋ธ๋ฐ~
// js ํจ์ : submit()
// http://localhost/webPro/javascript/days04
// /sample01_ok.jsp?ํ๋ผ๋ฏธํฐ X
// ?selsubject=eng
// document.querySelector("form").submit();
// document.forms ์ปฌ๋ ์
, options ์ปฌ๋ ์
document.forms[0].submit();
}
[sample01_ok.jsp]
-html-
<%
// ?selsubject=eng
String subject = request.getParameter("selsubject");
// [ jsp ์คํฌ๋ฆฝํธ 3๊ฐ์ง ์ข
๋ฅ ]
// 1) ์คํฌ๋ฆฝํธ๋ฆฟ
%>
<!-- 2) ํํ์ ( ์ถ๋ ฅ )-->
subject = <%= subject %><br>
[ ์ ํ๋ ๊ณผ๋ชฉ ] <br>
<input type="radio" name="subject" value="kor"><label>๊ตญ์ด</label><br>
<input type="radio" name="subject" value="eng"><label>์์ด</label><br>
<input type="radio" name="subject" value="mat"><label>์ํ</label><br>
<input type="radio" name="subject" value="pe"><label>์ฒด์ก</label><br>
-js-
var svalue = "<%= subject %>" ; // js ๋ ๋ฌธ์์ด์ ์ ํ ๋ฐ์ดํ
var radios = document.getElementsByName("subject");
for (var i = 0; i < radios.length; i++) {
if( radios[i].value == svalue ){
radios[i].checked = true;
break; // return;
} // if
} // for
-jquery-
$(":radio[value='<%= subject %>']").prop("checked", true);
* ์ฃผ์ : <%-- JSP ์ฃผ์์ฒ๋ฆฌ ***
2. โถ, โถโถ ๋ฒํผ์ ๊ตฌํํ์ธ์
<style>
select{
height: 200px;
width: 100px;
}
button{
width:40px;
}
</style>
<table>
<tr>
<td>
<select multiple="multiple" id="sleft">
<option>์ด์ฐฝ์ต</option>
<option>๊น๊ฐ์จ</option>
<option>์์ข
์ค</option>
<option>๋ฌธํ๋น</option>
<option>์ํํ</option>
</select>
</td>
<td>
<button>โถ</button><br>
<button>โถโถ</button><br>
<button>โ</button><br>
<button>โโ</button><br>
</td>
<td>
<select multiple="multiple" id="sright">
</select>
</td>
</tr>
</table>
-js-
<script>
// <button>โถ ํด๋ฆญ
document.querySelector("button:first-of-type").onclick = function (){
// 1. sleft ์์ ์ ํ๋(selected) option์ text, value ์ป์ด์์ผ...
var sleftOptions = document.querySelectorAll("#sleft option");
var sright = document.querySelector("#sright");
// 2. sright ๋ชจ๋ option ์ ๊ฑฐ
// sright.options.length=0;
sright.innerHTML = "";
for (var i = 0, j=0; i < sleftOptions.length; i++) {
if( sleftOptions[i].selected ){
// console.log( sleftOptions[i].value +" / " + sleftOptions[i].text );
// sright + option ์ถ๊ฐ
// sright.options[j++] = new Option(sleftOptions[i].value,sleftOptions[i].value);
// js ๋ณต์ : cloneNode()
sright.appendChild( sleftOptions[i].cloneNode(true) );
}
} // for
} //
// select , option ์์ ์์ฑ, ์ ๊ฑฐ
// createElement() / ์ ๊ฑฐ ๋ฉ์๋
// 1) sleft.remove(index); ๋ฐ๋์ ๋ค์ ์ธ๋ฑ์ค ->
// <button>โถโถ ํด๋ฆญ
document.querySelector("button:nth-of-type(2)").onclick = function (){
// 1. sleft ์์ ์ ํ๋(selected) option์ text, value ์ป์ด์์ผ...
var sleftOptions = document.querySelectorAll("#sleft option");
var sright = document.querySelector("#sright");
//sright.innerHTML = "";
// sright.options.length=0;
for (var i = 0, j=0; i < sleftOptions.length; i++) {
if( sleftOptions[i].selected ){
// console.log( sleftOptions[i].value +" / " + sleftOptions[i].text );
// sright + option ์ถ๊ฐ
// sright.options[j++] = new Option(sleftOptions[i].value,sleftOptions[i].value);
// jquery method : append()/ appendTo()
// js method : appendChild()
sright.appendChild( sleftOptions[i] );
}
} // for
} //
</script>
-jquery-
<script>
// <button>โถ ํด๋ฆญ
$("button").first().click( function (){
// alert("a")
$("#sleft option:selected").each(function(i, element) {
// jquery ๋ณต์ ๋ฉ์๋ : clone()
$("#sright").append( $(element).clone() );
});
});
// <button>โถโถ ํด๋ฆญ
$("button").eq(1).click( function (){
// alert("b")
$("#sleft option:selected").each(function(i, element) {
$("#sright").append(element);
});
});
</script>
6. ์ฒซ ๋ฒ์งธ ํ ์คํธ ๋ฐ์ค์ ์ซ์๋ฅผ ์ ๋ ฅ ํ ์ํฐ ์น๋ฉด ๋ ๋ฒ์งธ ํ ์คํธ ๋ฐ์ค๋ก ํฌ์ปค์ค ๋๊ธฐ๊ณ
๋ ๋ฒ์งธ ํ ์คํธ ๋ฐ์ค์ ์ซ์๋ฅผ ์ ๋ ฅ ํ ์ํฐ ์น๋ฉด ๋ง์ ๊ฒฐ๊ณผ๋ฅผ ์ธ ๋ฒ์งธ ํ ์คํธ ๋ฐ์ค์ ์ถ๋ ฅํ๋ js ์ฝ๋ฉ์ ํ์ธ์.
<input type="text" autofocus="autofocus" >
<!-- onkeydown="txt01_keydown();" -->
+
<input type="number" >
=
<!-- 12e+5 -->
<input type="text" disabled="disabled">
-js-
function txt01_keydown(){
// console.log( event.keyCode ); 48(0) ~ 57(9)
if( !( event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 8 || event.keyCode == 13 || event.keyCode == 229 ) ){
alert("์
๋ ฅ ์๋ชป!!!");
event.returnValue = false ; // ์ด๋ฒคํธ ์ทจ์ + ์ถ๋ ฅ O
}
if( event.keyCode == 13 ){
document.querySelector("input[type=number]").focus();
}
}
document.querySelector("input[type=number]").onkeyup = function (){
// alert("xxxx");
if( event.keyCode == 13 ){
var n1 = document.querySelector("input[type=text]:first-of-type").value;
// alert( typeof n1); // js : number [string] function boolean object
var n2 = this.value;
// "12" -> 12 ํ๋ณํ
// java : Integer.parseInt( "12" )
// js : Number(), parseFloat(), parseInt(), eval()
var sum = Number(n1) + parseInt(n2); // "12"+"23" = "1223"
document.querySelector("input[type=text]:last-of-type").value = sum;
}
}
-jquery-
// $("input[type=text]")
$(":text").first().keydown(function() {
if ( event.keyCode == 13 ) {
$(this).next().focus();
} //
})
$("input[type=number]").keydown(function() {
if ( event.keyCode == 13 ) {
var n1 = eval( $(":text").first().val() );
var n2 = parseInt( $(this).val() );
var sum = n1 + n2;
// $(":text").last()
$(this).next().val( sum );
} //
})
<style>
table, th, td{
border: 1px solid gray;
}
table{
width: 50%;
margin: 0 auto;
}
</style>
<table>
<thead>
<tr>
<td><input type="checkbox" class="all">๋ชจ๋ ์ ํ</td>
<td>Name</td>
<td>Loc</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" id="ck1"></td>
<td>Animi.</td>
<td>Hic.</td>
</tr>
<tr>
<td><input type="checkbox" id="ck2"></td>
<td>Esse.</td>
<td>Fuga.</td>
</tr>
<tr>
<td><input type="checkbox" id="ck3"></td>
<td>Fuga.</td>
<td>Ducimus?</td>
</tr>
<tr>
<td><input type="checkbox" id="ck4"></td>
<td>Beatae.</td>
<td>Optio.</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3" align="center">
<button>์ ํ ํ์ธ</button>
</td>
</tr>
</tfoot>
</table>
<p id="demo">
"์ ํ ํ์ธ" ๋ฒํผ์ ํด๋ฆญํ ๋ ์ฒดํฌ๋ฐ์ค์ id ์์ฑ๊ฐ + [Name ์ปฌ๋ผ]์ ๊ฐ๋ ๋ชจ๋ ์ถ๋ ฅ
ck1, ch3, ch4
</p>
-js-
<script>
document.querySelector("tfoot button").onclick = function (){
// alert("1");
// document.querySelectorAll("input[type=checkbox].not(.all)")
var ckbs = document.querySelectorAll("tbody input[type=checkbox]");
var pdemo = document.querySelector("#demo");
var content = "";
for (var i = 0; i < ckbs.length; i++) {
if( ckbs[i].checked ){
// DOM ์์(๋
ธ๋) ํ์
// console.log( ckbs[i].parentElement.parentElement.childNodes ); // ๋
ธ๋ ์ปฌ๋ ์
var name = ckbs[i].parentElement.parentElement.children[1].innerText ; // ์์ ์ปฌ๋ ์
// ckbs[i].parentNode
content += ckbs[i].id + " / " + name + "<br>";
}
} // for
pdemo.innerHTML = content;
}
</script>
-jquery-
<script>
$("tfoot button").click( function (){
// jquery selector :checkbox == input[type=checkbox]
var content = "";
$("tbody :checkbox:not(.all):checked").each(function(i, element) {
var id = element.id ;
var name = element.parentElement.nextElementSibling.innerText;
content += "<li>"+ id + " / " + name + "</li>";
/*
$("#demo").html( function ( index, oldhtml){
// return oldhtml +"<li>"+ id +" / " + name + "</li>";
return oldhtml + $("<li></li>").text( id +" / "+ name );
});
*/
});
$("#demo").html( content ) ;
} );
</script>
-๋ชจ๋ ๋ฐ์ค ์ฒดํฌ๋๋ฅด๋ฉด ๋ค ์ฒดํฌ & ์ฒดํฌ ํ๋ ํ๋ฉด ๋ชจ๋์ฒดํฌ ํ๊ธฐ-
<script>
$(":checkbox.all").click( function (){
$(":checkbox:not(.all)").prop("checked", $(this).prop("checked") );
});
$(":checkbox:not(.all)").click( function (){
// ์ฒดํฌ๋ ์ฒดํฌ๋ฐ์ค์ ๊ฐฏ์
var totalCount = $(":checkbox:not(.all)").length;
var checkedCount = $(":checkbox:not(.all):checked").length;
$(":checkbox.all").prop("checked", checkedCount == totalCount ? true: false);
/*
if( checkedCount == totalCount ){
$(":checkbox.all").prop("checked", true);
}else{
$(":checkbox.all").prop("checked", false);
}
*/
});
</script>
js ํจ์
1. js ํจ์ ๊ฐ๋ == ์๋ฐ ํจ์ ๊ฐ๋
- ์ (line) ๊ธ๊ธฐ : 50๋ฒ X 50์ค์ฝ๋ฉ drawLine() ์ ์ธ -> ํธ์ถ, ์ค๋ฒ๋ก๋ฉ(์ค๋ณตํจ์)
2. js ํจ์ ์ ์ธ ํ์
function ํจ์๋ช
([๋งค๊ฐ๋ณ์...]){
[return ๋ฆฌํด๊ฐ;]
}
3. js ํจ์ ํธ์ถ 3๊ฐ์ง ๋ฐฉ๋ฒ..
1) on์ด๋ฒคํธ๋ช ์์ฑ = "jsํจ์();"
2) js ์ฝ๋์์ jsํจ์ ํธ์ถ
3) ์๋( ์์ฒด ํธ์ถ) X -> "js ํด๋ก์ "
<button onclick="btn1_click();">๋ฒํผ 1</button><br>
<button onclick="btn2_click();">๋ฒํผ 2</button><br>
<script>
// ๋ ์ ์์ ํฉ์ ๋ฐํํ๋ sum ํจ์ ์ ์ธ
/*
public static int sum(int a, int b){
return a+b;
}
*/
function sum(a,b){
// ํจ์ ์์ ์ ์ธ๋ ๋ณ์ : ์ง์ญ๋ณ์
return a+b;
}
function btn1_click(){
var result = sum(10,20);
alert( result );
}
function btn2_click(){
//
//
// btn1_click();
// js ์๋ฃํ : number, string, boolean, function, object
//var fnhap = sum;
var fnhap = function (a, b){
return a+b;
}
var result = fnhap(1,2);
alert( result );
}
// btn1_click(); // ํจ์ ํธ์ถ
(function (){ // ์ต๋ช
ํจ์
//
//
})();
</script>
function btn1_click(){
// var name = "ํ๊ธธ๋"; // ํจ์ ์์์๋ง ์ฌ์ฉ๋๋ ์ง์ญ๋ณ์
name = "์ค์น์ฃผ"; // var, let, const, X ์ ์ญ๋ณ์
alert( "1 : " + name );
}
function btn2_click(){
alert( "2 : " + name );
// alert( "2 : " + window.name );
}
undefined์ null ๋น๊ต
var msg;
// if( msg == null ){}
// alert( typeof msg ); // undefined ํ์
// alert( typeof null); // object ํ์
// alert( undefined == null ); // true
// alert( undefined === null ); // ๊ฐ + ํ์
//var person ; // undefined ํ์
//var person = null; // object ํ์
var name = ""; // string ํ์
var name ; // undefined ํ์
== ์์ง ๊ฐ์ด ํ ๋น๋์ง ์์ ํ์
์ด ๊ฒฐ์ ๋์ง ์์๋ค.
js ๊ฐ์ฒด(๊ฐ์ฒด,object)
// 1. js ๊ฐ์ฒด {} ์ค๊ดํธ - ๋ฐฐ์ด []๋๊ดํธ
// js ๊ฐ์ฒด๋ ์์ฑ์ด๋ผ๊ณ ํ๋ ๋ช
๋ช
๋ ๊ฐ๋ค์ ์งํฉ(์ปจํ
์ด๋)์ด๋ค.
const person = {
name:"admin" ,
age:20,
dispInfo:function (){
return this.name + ", " + this.age;
// ์ด๋ this๋ js ๊ฐ์ฒด ์๊ธฐ ์์ ์ ๋ํ๋ธ๋ค.
}
};
console.log( person.name ); // objectName.propertyName;
console.log( person["name"] ); // objectName["propertyName"]
console.log( person.dispInfo() );
์๊ฒฉ๋ชจ๋ ์ ์ธ (๋ณ์๋ ๊ผญ ์ ์ธํ๊ณ ์ฌ์ฉํ๊ฒ ๋ค)
"use strict";
this์ ์ฌ์ฉ์ฒ ๋ฐ๋ฅธ ์๋ฏธ
- js ์์๋ [ this ํค์๋ ]๊ฐ ์ฌ์ฉ๋๋ ๊ณณ(๋ฐฉ๋ฒ)์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ( ๊ธฐ์ต )
//(1) [object Window] BOM ์ต์์ ๊ฐ์ฒด
alert( this );
// (2) ํจ์ ์์์ this [object Window]
// ์๊ฒฉ๋ชจ๋ this [undefined]
function test(){
alert( this );
}
test();
// (3) ์ด๋ฒคํธ ํธ๋ค๋ฌ(์์ ๊ธฐ)์์ this
function h3_click(){
alert( this )
}
// (4) person ๊ฐ์ฒด ๋ด์ this ๋ ๊ฐ์ฒด ์๊ธฐ ์์ ( object )
var person = {
name:"admin"
, dispInfo: function (){
// return this.name ;
alert( this ); // [object Object]
}
}
person.dispInfo(); // [object Object]
<!-- ์ด๋ this๋ ์ด๋ฒคํธ๋ฅผ ๋ฐ์ ๊ฐ์ฒด๋ฅผ ๋ํ๋ธ๋ค. [object HTMLHeadingElement] -->
<h3 onclick="alert( this );">ex05.html - ์๊ฒฉ๋ชจ๋ ์ ์ธ, this์ ์ฌ์ฉ์ฒ ๋ฐ๋ฅธ ์๋ฏธ, ์ด๋ฒคํธ ์ฒ๋ฆฌ</h3>
์ด๋ฒคํธ ์ฒ๋ฆฌ
1) on์ด๋ฒคํธ๋ช =""
2) document.getElementById("demo").on์ด ๋ฒคํธ๋ช = function (){}
3) document.getElementById("demo").addEventListener("click", function(e) { } );
js ๋ฌธ์์ด
var msg = "AB\"CD\"EFGHIJKLMNOPQRSTUVWXYZ";
// 1. ๋ฌธ์์ด ๊ธธ์ด - length ์์ฑ
console.log( "1. msg ๋ฌธ์์ด ๊ธธ์ด : " + msg.length ); // 28
// 2. ๋ ๋ฌธ์์ด ๋น๊ต - == != === !== ์ฐ์ฐ์.
var name1 = "ํ๊ธธ๋"; // string
// var name2 = "ํ๊ธธ๋";
var name2 = new String("ํ๊ธธ๋"); // object // String
alert( typeof name1); // string
alert( typeof name2); // object
// java equals()
// js ==
console.log( name1 == name2 ); // true true
console.log( name1 === name2 ); // true ๊ฐ + ํ์
false !==
๊ฐํ์ ์ด๋ฌธ์
\n \t \b \f \r |
๋ฉ์๋
var msg = " hello world ";
// console.log( "["+ msg.trim() +"]" );
var str1 = "hello", str2 ="world";
// ๋ ๋ฌธ์์ด ์ฐ๊ฒฐ(๊ฒฐํฉ) : + ๋ฌธ์์ด ์ฐ๊ฒฐ ์ฐ์ฐ์.
// var str3 = str1 + " " + str2;
// var str3 = str1.concat("-").concat(str2);
var str3 = str1.concat("-", str2, "์ฐ๊ฒฐ๋ฌธ์์ด");
// console.log( str3 );
๋๋ฌธ์๋ณํ
msg = "Hello World!!!";
// ๋๋ฌธ์ ๋ณํ
//console.log( msg.toUpperCase() ); // HELLO WORLD!!!
//console.log( msg.toLowerCase() ); // hello world!!
ํ์์ด๋ฆ :
<input type="text" value="๊น๊ฐ์จ(ํ์ฅ), ์กฐ๋ฏผ๊ฒฝ(๊ธฐ์ ๊ณ ๋ฌธ), ์ํฌ์ง, ์ด์ฌ๋ฏผ, ์ํํ, ํฉ์๋น, ๋ฐํ์">
<div id="demo">
<!-- <li>๊น๊ฐ์จ</li>
<li>์กฐ๋ฏผ๊ฒฝ</li> -->
</div>
// :text jquery selector
document.querySelector("input[type=text]").onkeydown = function (){
if( event.keyCode == 13 ){
// 1. ํ
์คํธ๋ฐ์ค์์ ์
๋ ฅ๋ฐ์ ํ์๋ช
์ ์ป์ด์์.
var team = this.value;
// console.log( team );
// 2. ๊ตฌ๋ถ์ ์ฝค๋ง(,)๋ก ๋ฌธ์์ด์ ์๋ผ๋ด๊ธฐ.
// 3. js ๋ฐฐ์ด ์ ์ฅ
var teamList = team.split(",");
// 4. js ๋ฐฐ์ด์ for/ ๋ฐ๋ณต๋ฌธ ์ฌ์ฉํด์ <li>๊น๊ฐ์จ</li>
// 5. <div id="demo"> ์์์ผ๋ก ์ถ๊ฐ, innerHTML ์ค์ .
var demo = document.querySelector("#demo");
/*
// DOM
for (var i = 0; i < teamList.length; i++) {
// console.log( teamList[i].trim() );
var li = document.createElement("li");
li.innerText = teamList[i].trim();
demo.appendChild( li );
} //for
*/
var content = "<ol>";
for (var i = 0; i < teamList.length; i++) {
content += "<li>";
content += teamList[i].trim();
content += "</li>";
} //
content += "</ol>";
// <ol><li>๊น๊ฐ์จ(ํ์ฅ)</li><li>์กฐ๋ฏผ๊ฒฝ(๊ธฐ์ ๊ณ ๋ฌธ)</li><li>์ํฌ์ง</li><li>์ด์ฌ๋ฏผ</li><li>์ํํ</li><li>ํฉ์๋น</li><li>๋ฐํ์</li></ol>
// alert( content );
demo.innerHTML = content;
js ์ ๊ทํํ์
// 1. ์๋
์์ผ
// 2. ์ฑ๋ณ
// 3. ๋ง๋์ด
// 4. ๊ฒ์ฆ
var rrn = "781222-170001";
// console.log( rrn.length ); // 14
// ์ซ์6-์ซ์7 true/false rrn.matches( ์ ๊ทํํ์)
// [ js ์ ๊ทํํ์ ] - ๋ฌธ์์ด ๋ฉ์๋ : search(), replace(), test(), exec()
// ํ์) /pattern/modifiers
// /[0-9]{6}-\d{7}/i [i,g,m]
// pattern = /์ ๊ทํํ์/๋ชจ๋ ;
// boolean pattern.test( ๋ฌธ์์ด)
var pattern = /^[0-9]{6}-\d{7}$/ ;
// console.log( pattern.test(rrn) );
// /^[0-9]{6}-\d{7}$/.test( rrn )
// ์์ผ ๋
๋
// ์์ผ ์
//console.log( rrn.substring(2, 4) ); // to
//console.log( rrn.substr(2, 2) ); // length
//console.log( rrn.slice(2, 4) );
// ์ฃผ๋ฏผ๋ฑ๋ก๋ฒํธ ๋ท์๋ฆฌ 7 ์ถ๋ ฅ.
console.log( rrn.substring( 7 ) ); // 170001
console.log( rrn.substring( 7, 14 ) ); // 170001
console.log( rrn.substring( 7, rrn.length ) ); // 170001
console.log( rrn.substr(7) ); // from [, length]
console.log( rrn.substr(7, 7) ); // from , length
console.log( rrn.slice(7) );
console.log( rrn.slice(7, 14) ); // from , to
// ์ฐจ์ด์
console.log( rrn.slice(-6) ); // ์์:๋ท ์๋ฆฌ์
// ์์ผ ์ผ
// ์ฑ๋ณ
//rrn.substring(from, to);
//rrn.substr(from, length);
// rrn.slice(from, to);
<script>
var input = "\nIs Th \nis it?";
// var pattern = /^is/;
// var pattern = /^is/i;
var pattern = /^is/m; /* [m]ultiline */
//console.log( pattern.test( input ) );
</script>
<script>
var input = "Visit Microsoft!";
// var pattern = "Microsoft"; // string
// var pattern = "microsoft";
var pattern = /microsoft/i; // regexp
var index = input.search(pattern);
//console.log( index ); // 6 , -1
//console.log( input.replace(pattern, "XXX") ); // Visit XXX!
</script>
js ๋ฌธ์์ด ๋ฉ์๋
var msg = "์๋
ํ์ธ์, ํ๊ธธ๋ ์
๋๋ค. -ํ๊ธธ๋ ์ - ";
// var pattern = "ํ๊ธธ๋"; // string
var pattern = /ํ๊ธธ๋/g ; // regexp i m g
var replacement = "YYY";
console.log( msg.replace(pattern, replacement) );
// msg.replaceAll(target)
// msg.indexOf("ํ๊ธธ๋", 0);
console.log( msg.indexOf("ํ๊ธธ๋") ); // 7
// msg.lastIndexOf(elt, from)
//console.log( msg.charAt(0) );
// console.log( msg.charAt(1) );
for (var i = 0; i < msg.length; i++) {
// console.log( msg.charAt(i) );
}
// "7" -> 7 ํ๋ณํ ? Java : Ineger.parseInt()
// js : Number() , parseInt(), parseFloat(), eval()
// 7 -> "7" ํ๋ณํ ? 7 + "" "7", new String(7)
/*
let no = 7 + "";
// "0007" String.format("%04d", 7) "0007" StringFormat
no = no.padStart(4, "0");
console.log( no ); // "0007"
*/
let no = "17";
var len = no.length ;
for (var i = 0; i < len ; i++) {
no = "0" + no;
}
// console.log( i + " : " + no );
// padStart() , padEnd() ์๋ก ์ถ๊ฐ๋ ๋ฉ์๋(2017) - ES5(2015)
var msg = " hello world ";
console.log( "["+ msg.trim() +"]" ); // [hello world]
console.log( "["+ msg.trimStart() +"]" ); // [hello world ]
console.log( "["+ msg.trimEnd() +"]" ); // [ hello world]
์ต๊ทผ๋๊ธ