Javascriptๆๅญๅใฎใกใฝใใ : slice(), substring(), includes(), startsWith(), trim(), replace()…….
ใใญใณใใจใณใ(React, Vue, Angular)ใใใใฏใจใณใ(Nodejs)ใใธใฃใในใฏใชใใ(Javascript)ใไฝฟใใใๆไปฃใซใชใฃใฆใใ ใใ ใใธใฃใในใฏใชใใใฎๆๆณใ้่ฆใจใชใใพใใญใ
ใใฎไธญใๆๅญๅ(String)้ข้ฃใฎใกใฝใใใฏใใไฝฟใใฎใซใใใพใ่ฆใใฆใฏใใชใใใคใงใใญใ๐
ๆๅญๅ้ข้ฃใฎใกใฝใใใฏไธๅบฆ่ฆใใฆใใใจ่ชๅใซ่ฆใใใใใใใไฝฟใ้ ปๅบฆใ้ซใใฎใงๆๅใ ใ้ ๅผตใใฐใงใใญ๏ผ
ใงใฏใ้ ป็นใซไฝฟใใกใฝใใใฎไธญslice(), substring(), includes(), startsWith(), trim(), replace(), indexOf(), test()ใใ็ดนไปใใพใใ
sliceใกใฝใใใฏใคใณใใใฏในใใใฉใกใผใฟใงๆธกใใๅฟ ่ฆใชๆๅญๅใๆฝๅบใใพใใ
ในใฟใผใใคใณใใใฏใน(start index)ใจ็ตไบใคใณใใใฏใน(end indexใ็็ฅๅฏ)ใๆธกใใ็ตไบใคใณใใใฏในใ็็ฅใใๅ ดๅใฏๆๅญๅใฎๆๅพใพใงใซใชใใพใใ
ใใฉใกใผใฟใ๏ผใฎๅ ดๅใฏ้ ญใใไธ็ช็ฎใ-1ใฏ้ใงๅพใใใไธ็ช็ฎใฎๆๅณใซใชใใพใใ
const myWord = 'primavera'; myWord.slice(0,5); // 'prima' myWord.slice(-4); // 'vera' myWord.slice(3); // 'mavera' myWord.slice(3,1); // '' -> slice()ใฏในใฟใผใใ็ตไบใใๅคงใใใจ็ฉบใ่ฟใ
substringใกใฝใใใsliceใจๅใใใคใณใใใฏในใๆธกใใๆๅญๅใๆฝๅบใใพใใ
ๅบๆฌ็ใชๆไฝใฏๅๆงใงใใsubstringใฏในใฟใผใใ็ตไบใใๅคงใใใฎๅ ดๅ่ชๅใซไฝ็ฝฎใไบคๆใใฆใกใฝใใใๅฎๆฝใใพใใ
ใพใใsubstringใซ่ฒ ๆฐใๅ ฅใใใจ๏ผใจ่ช่ญใใพใใ
ไพใใฐ(3, -1)ใๆธกใใจ(3, 0)ใจใใฆ่ช่ญใใพใใใใใซ(3, 0)ใฏในใฟใผใใๅคงใใใฎใง่ชๅไบคๆใใ(0, 3)ใๅฎๆฝใใพใใ
ใกใฝใใๅใไผผใใใใชsubstrใฏโไฝ็ฝฎโใจโๆๅญๆฐ’ใใใฉใกใผใฟใซๆธกใใกใฝใใใงใใ
const myWord = 'invierno'; myWord.substring(3); // 'ierno' myWord.substring(1,3); // 'nv' myWord.substring(3,1); // 'nv' -> ่ชๅใซในใฟใผใใจ็ตไบไฝ็ฝฎไบคๆ myWord.substring(3,-1); // 'inv' -> ่ฒ ๆฐใฏ0, ่ชๅไฝ็ฝฎไบคๆ myWord.substring(-4); // 'invierno' -> ่ฒ ๆฐใฏ0, ในใฟใผใ(0)ใใๆๅพใพใง myWord.substr(1,3); // 'nvi' -> ในใฟใผใ1ใใไธใค
includesใฏๆๅญๅใฎไธญใๆๅฎใใๆๅญๅใๅซใพใใฆใใใใใใงใใฏใใพใใ
ใใฉใกใผใฟใฏๆๅญๅ(ๅฟ ้ )ใจใคใณใใใฏใน(็็ฅๅฏ)ใๆธกใใ่ฟใใฎใฏtrueๆใฏfalseใงใใ
ใใฎใกใฝใใใฏๆๅฎๆๅญใฏใใกใใใ้ ๅใฎไธญใง็ฉบใฎๅคใ็ขบ่ชใใใใใซใฏไฝฟใใพใใ
const myWord = 'alegria'; myWord.includes('le'); // true myWord.includes('i'); // true myWord.includes('rio'); // false const myArray = ['dream','','king']; myArray.includes(''); // true -> myArray[1]ใ็ฉบใชใฎใงtrue myArray.includes('dream'); // true -> myArray[0]ใจไธ่ด myArray.includes('drea'); // false -> ่ฆ็ด ใฏๅฎๅ จไธ่ดใใๅ ดๅใฎใฟtrue
startsWithใกใฝใใใๆๅญๅใๅซใพใใฆใใใใใใงใใฏใใพใใ
ๆๅญๅใฎใฟๆธกใใจ0ใคใณใใใฏในใใๅงใพใๆๅฎๆๅญใฎๅญๅจใ็ขบ่ชใใBOOLEANใ่ฟใใพใใ
endsWithใฏๆๅฎๆๅญใง็ตใใใใจใใใงใใฏใใพใใ
const mySentence = 'cafe con leche'; mySentence.startsWith('cafe'); // true mySentence.startsWith('fe'); // false mySentence.startsWith('fe',2); // true mySentence.endsWith('eche'); // true
trimใฏๆๅญๅไธกๆญใฎ็ฉบ็ฝใ้คใใกใฝใใใงใใ
ๆๅญๅๅคๆใฎ้็จใงๅ ฅใใใ็ฉบ็ฝใพใใฏใฟใคใใในใงๅ ฅใฃใๆๅญๅใฏๆๅญๆฏ่ผใฎๆfalseใ่ฟใใใจใฉใผใซใชใใใกใงใใ
ใชใฎใงtrimใง็ฉบ็ฝใไบๅใซ้คใใใจใฉใผใไบ้ฒใใใใจใใงใใพใใ
ๆๅญๅไธกๆญใฎ็ฉบ็ฝใใฟใใใๆน่ก(/n)ๆๅญใ้คใใใจใใงใใพใใใไธกๆญใงใฏใชใๆๅญๅไธญใซๅ ฅใฃใฆใใใฎใฏไฝๅใใพใใใ
const mySentence = ' la casa de papel '; mySentence.trim(); // 'la casa de papel' const mySentence = ' el amor \n'; mySentence.trim(); // 'el amor'
replaceใฏๆๅญๅใใๆๅฎใใๆๅญใๆขใใฆๅคๆๆๅญใซๅคๆใใพใใ
ๆๅญๅใฎไธญใซๆๅฎๆๅญใไธใคไปฅไธๅญๅจใใใจใใๆๅใซ่ฆๅฝใใใฎใ ใใๅคๆดๅฏพ่ฑกใซใชใใพใใ
ไพใใฐ ‘my_name_is_Ron’ใใใขใณใใผใใผใๅ จ้จ็ฉบ็ฝใซใใใใreplace(‘_’,’ ‘)ใซใใฆใ็ตๆใฏโmy name_is_Ron’ใซใชใใพใใ
ใชใฎใงใๆๅญๅใฎไธญใงๅญๅจใใๅ จใฆใๆๅฎๆๅญใๅคๆใใใๆใฏๆญฃ่ฆ่กจ็พใไฝฟใใจ่กใใพใใ
const mySentence = 'my name is:Ron'; mySentence.replace(':',' '); //'my name is Ron' const mySentence = 'my_name_is_Ron'; mySentence.replace('_',' '); // 'my name_is_Ron' //ๆญฃ่ฆ่กจ็พใงใในใฆใฎๆๅญใๅคๆ mySentence.replace(/_/g, ' '); // 'my name is Ron'
indexOfใฏๆๅฎๆๅญใฎไฝ็ฝฎใ่ฟใใพใใ
ใใฎใกใฝใใใฏๆๅฎๆๅญใฎๅใพใใฏๅพใงๆๅญๅใๅใๅใใจใๆ็จใชใใฎใงใใ
ใใจใใ’Z001-03’ใใๅพใใฎ’03’ใ ใใๆฝๅบใใใใจใใindexOfใง’-‘ๆๅญใๆขใใ่ฟใใฎindex +1ไฝ็ฝฎใใๆๅพใพใงใซใใใใใจ’03’ใๅใๅใใใจใใงใใพใใ
ใใๆๅญๅใฎไธญๆๅฎๆๅญใไธใคไปฅไธใฎๅ ดๅใ้ ญใใๆขใๅงใๆๅใซ่ฆๅฝใใๆๅญไฝ็ฝฎใ่ฟใใพใใ
ใงใใใฉใกใผใฟใซindexใๆธกใใจๆๅฎใใใไฝ็ฝฎใใๆๅฎๆๅญใๆขใใพใใ
ๆณจๆ็นใฏstart indexใๆธกใใฆใ่ฟใๅคใฏๅ จไฝใฎๆๅญๅใใไฝ็ช็ฎใงใใใจใใๆฐๅญใ่ฟใใพใใ
ๅพใใใๆค็ดขใใใฎใฏlastIndexOfใงใๅพใใใๆๅใฎๆๅฎๆๅญใฎไฝ็ฝฎใ่ฟใใพใใ
ใใฎๆใ่ฟใๅคใฏ้ ญใใไฝ็ช็ฎใงใใใจใใๆฐๅญใงใใ
ๆๅฎๆๅญใใชใใใฐ-1ใ่ฟใใพใใ
const mySentence = 'Z001-03'; mySentence.indexOf('-'); // 4 mySentence.substring(mySentence.indexOf('-')+1); // '03' mySentence.indexOf('D'); // -1 const mySentence = 'Z001-03-02'; mySentence.indexOf('-'); // 4 mySentence.indexOf('-', 5); // 7 mySentence.lastIndexOf('-') // 7
testใฏไธ่ฌ็ใซๆญฃ่ฆ่กจ็พใจไธ็ทใซไฝฟใใๆธกใๆๅญๅใจๆฏ่ผใๆๅนๆงใ็ขบ่ชใใพใใ
ใในใฏใผใใใขใซใใกใใใใ็นๆฎๆๅญใ็ๅนดๆๆฅใชใฉใฎๅฝขๅผๆคๆปใซไฝฟใใพใใ
const mySentence = /a/; const myWord = 'baby'; mySentence.test(myWord); // true /\d/.test('cookie'); // false \dใฏๆฐๅญใๆๅณ(ๆญฃ่ฆ่กจ็พ) /\d/.test('cookie1'); // true
ไธใฎไพไปฅๅคใซใๅฑฑใปใฉใกใฝใใใๅคใใใใพใๆฐใใไฝใใใใกใฝใใใใใใใใใใพใใญใ
ใพใๅฟ ่ฆใชๆฉ่ฝใฎใกใฝใใใใใใฐ็ขบ่ชใใฆใใใพใใ๏ผ
ใใใผใ๏ผ