JS กวดวิชา

เจเอสโฮม JS Introduction JS Where To JS เอาท์พุต งบ JS JS ไวยากรณ์ JS ความคิดเห็น ตัวแปร JS เจเอส เลท JS Const JS Operators JS เลขคณิต JS Assignment ประเภทข้อมูล JS ฟังก์ชัน JS วัตถุ JS JS Events JS Strings JS String Methods JS String Search เทมเพลตสตริง JS JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Array Iteration JS Array Const JS Dates รูปแบบวันที่ JS JS วันที่รับเมธอด JS Date Set Methods JS Math JS สุ่ม JS Booleans JS เปรียบเทียบ JS Conditions JS Switch JS ลูปสำหรับ JS Loop สำหรับ In JS วนสำหรับ Of JS วนรอบในขณะที่ JS Break JS Iterables JS ชุด JS Maps JS Typeof JS ประเภทการแปลง JS Bitwise JS RegExp JS Errors JS ขอบเขต JS Hoisting JS โหมดเข้มงวด JS คำสำคัญนี้ JS Arrow Function JS Classes JS JSON JS Debugging คู่มือสไตล์ JS JS แนวทางปฏิบัติที่ดีที่สุด JS ความผิดพลาด JS Performance JS สงวนคำ

เวอร์ชัน JS

เวอร์ชัน JS JS 2009 (ES5) เจเอส 2015 (ES6) JS 2016 JS 2017 JS 2018 JS IE / ขอบ JS History

วัตถุ JS

คำจำกัดความของวัตถุ คุณสมบัติของวัตถุ วิธีการวัตถุ การแสดงวัตถุ อุปกรณ์เสริมวัตถุ ตัวสร้างวัตถุ ต้นแบบวัตถุ วัตถุ Iterables ชุดวัตถุ แผนที่วัตถุ การอ้างอิงวัตถุ

ฟังก์ชัน JS

คำจำกัดความของฟังก์ชัน พารามิเตอร์ฟังก์ชัน เรียกใช้ฟังก์ชัน เรียกฟังก์ชัน ฟังก์ชัน สมัคร การปิดฟังก์ชัน

JS Classes

แนะนำคลาส การสืบทอดคลาส คลาสคงที่

JS Async

JS Callbacks JS แบบอะซิงโครนัส JS Promises JS Async/รอ

JS HTML DOM

บทนำ DOM วิธี DOM เอกสาร DOM องค์ประกอบ DOM DOM HTML แบบฟอร์ม DOM DOM CSS ภาพเคลื่อนไหว DOM กิจกรรม DOM DOM Event Listener การนำทาง DOM โหนด DOM DOM Collections รายการโหนด DOM

JS เบราว์เซอร์ BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

แนะนำ Web API เว็บฟอร์ม API API ประวัติเว็บ API ที่เก็บข้อมูลเว็บ Web Worker API API ดึงเว็บ เว็บ Geolocation API

JS AJAX

บทนำ AJAX AJAX XMLHttp คำขอ AJAX การตอบสนอง AJAX ไฟล์ AJAX XML AJAX PHP AJAX ASP ฐานข้อมูล AJAX แอปพลิเคชัน AJAX ตัวอย่าง AJAX

JS JSON

บทนำ JSON ไวยากรณ์ JSON JSON กับ XML ประเภทข้อมูล JSON JSON Parse JSON สตริงริฟาย วัตถุ JSON อาร์เรย์ JSON เซิร์ฟเวอร์ JSON JSON PHP JSON HTML JSON JSONP

JS กับ jQuery

ตัวเลือก jQuery jQuery HTML jQuery CSS jQuery DOM

JS กราฟิก

JS กราฟิก JS Canvas JS Plotly JS Chart.js JS Google Chart JS D3.js

ตัวอย่าง JS

ตัวอย่าง JS JS HTML DOM JS HTML อินพุต JS HTML Objects JS HTML Events JS Browser JS Editor JS แบบฝึกหัด JS Quiz ใบรับรอง JS

JS References

วัตถุ JavaScript วัตถุ HTML DOM


JavaScript String Search

วิธีค้นหา JavaScript

  • ดัชนีสตริงของ ()
  • สตริง lastIndexOf()
  • สตริง startWith()
  • สตริงสิ้นสุดด้วย ()

JavaScript สตริง indexOf()

วิธี การindexOf()ส่งกลับดัชนีของ (ตำแหน่งของ) การfirstเกิดของข้อความที่ระบุในสตริง:

ตัวอย่าง

let str = "Please locate where 'locate' occurs!";
str.indexOf("locate");

บันทึก

JavaScript นับตำแหน่งจากศูนย์

0 คือตำแหน่งแรกในสตริง 1 คือตำแหน่งที่สอง 2 คือตำแหน่งที่สาม ...


สตริง JavaScript LastIndexOf()

lastIndexOf()เมธอดส่งคืนดัชนีของการเกิด ขึ้น ล่าสุด ของข้อความที่ระบุในสตริง:

ตัวอย่าง

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate");

ทั้งสองindexOf()และlastIndexOf()ส่งคืน -1 หากไม่พบข้อความ:

ตัวอย่าง

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("John");

ทั้งสองวิธียอมรับพารามิเตอร์ที่สองเป็นตำแหน่งเริ่มต้นสำหรับการค้นหา:

ตัวอย่าง

let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15);

เมธอด จะlastIndexOf()ค้นหาย้อนหลัง (จากจุดสิ้นสุดไปยังจุดเริ่มต้น) ความหมาย: หากพารามิเตอร์ที่สองคือ15การค้นหาจะเริ่มต้นที่ตำแหน่ง 15 และค้นหาไปยังจุดเริ่มต้นของสตริง

ตัวอย่าง

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate", 15);

การค้นหาสตริง JavaScript ()

วิธี การsearch()ค้นหาสตริงสำหรับค่าที่ระบุและส่งกลับตำแหน่งของการจับคู่:

ตัวอย่าง

let str = "Please locate where 'locate' occurs!";
str.search("locate");

สังเกตไหม?

ทั้งสองวิธีindexOf()และsearch(), เท่ากันหรือไม่

พวกเขายอมรับอาร์กิวเมนต์เดียวกัน (พารามิเตอร์) และคืนค่าเดิมหรือไม่

ทั้งสองวิธีไม่เท่ากัน นี่คือความแตกต่าง:

  • เมธอด นี้search()ไม่สามารถใช้อาร์กิวเมนต์ตำแหน่งเริ่มต้นที่สองได้
  • เมธอด นี้indexOf()ไม่สามารถใช้ค่าการค้นหาที่มีประสิทธิภาพ (นิพจน์ทั่วไป)

คุณจะได้เรียนรู้เพิ่มเติมเกี่ยวกับนิพจน์ทั่วไปในบทต่อไป



การจับคู่สตริง JavaScript ()

เมธอด match() จะค้นหาสตริงสำหรับการจับคู่กับนิพจน์ทั่วไป และส่งกลับค่าที่ตรงกันเป็นออบเจกต์ Array

ตัวอย่าง 1

ค้นหาสตริงสำหรับ "ain":

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);

อ่านเพิ่มเติมเกี่ยวกับนิพจน์ทั่วไปในบทJS RegExp

บันทึก

หากนิพจน์ทั่วไปไม่มี ตัวแก้ไข g (เพื่อทำการ ค้นหา ทั่วโลก ) เมธอด match() จะส่งกลับเฉพาะรายการที่ตรงกันครั้งแรกในสตริง

ไวยากรณ์

string.match(regexp)
regexp Required. The value to search for, as a regular expression.
Returns: An Array, containing the matches, one item for each match, or null if no match is found

ตัวอย่าง 2

ทำการค้นหา "ain" ทั่วโลกโดยไม่พิจารณาตัวพิมพ์เล็กและตัวพิมพ์ใหญ่:

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);

สตริง JavaScript ประกอบด้วย ()

เมธอดคืน ค่าincludes()จริง หากสตริงมีค่าที่ระบุ

ตัวอย่าง

let text = "Hello world, welcome to the universe.";
text.includes("world");

ไวยากรณ์

string.includes(searchvalue, start)
searchvalue Required. The string to search for
start Optional. Default 0. Position to start the search
Returns: Returns true if the string contains the value, otherwise false
JS Version: ES6 (2015)

ตรวจสอบว่าสตริงมี "โลก" หรือไม่ โดยเริ่มการค้นหาที่ตำแหน่ง 12:

let text = "Hello world, welcome to the universe.";
text.includes("world", 12);

รองรับเบราว์เซอร์

includes()เป็นคุณลักษณะ ES6 (JavaScript 2015)

ได้รับการสนับสนุนในเบราว์เซอร์ที่ทันสมัยทั้งหมด:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

includes()ไม่รองรับใน Internet Explorer


สตริง JavaScript เริ่มด้วย ()

startsWith()เมธอดส่งคืนหากtrue สตริงเริ่มต้นด้วยค่าที่ระบุ มิฉะนั้นfalse:

ตัวอย่าง

let text = "Hello world, welcome to the universe.";

text.startsWith("Hello");

ไวยากรณ์

string.startsWith(searchvalue, start)

ค่าพารามิเตอร์

Parameter Description
searchvalue Required. The value to search for.
start Optional. Default 0. The position to start the search.

ตัวอย่าง

let text = "Hello world, welcome to the universe.";

text.startsWith("world")    // Returns false
let text = "Hello world, welcome to the universe.";

text.startsWith("world", 5)    // Returns false
let text = "Hello world, welcome to the universe.";

text.startsWith("world", 6)    // Returns true

บันทึก

startsWith()วิธีการนี้คำนึงถึงขนาดตัวพิมพ์


รองรับเบราว์เซอร์

startsWith()เป็นคุณลักษณะ ES6 (JavaScript 2015)

ได้รับการสนับสนุนในเบราว์เซอร์ที่ทันสมัยทั้งหมด:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

startsWith()ไม่รองรับใน Internet Explorer


สตริง JavaScript จบลงด้วย ()

endsWith()เมธอดจะคืนค่าหากtrue สตริงลงท้ายด้วยค่าที่ระบุ มิฉะนั้นfalse:

ตัวอย่าง

ตรวจสอบว่าสตริงลงท้ายด้วย "Doe":

var text = "John Doe";
text.endsWith("Doe");

ไวยากรณ์

string.endswith(searchvalue, length)

ค่าพารามิเตอร์

Parameter Description
searchvalue Required. The value to search for.
length Optional. The length to search.

ตรวจสอบอักขระ 11 ตัวแรกของสตริงที่ลงท้ายด้วย "world":

let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);

บันทึก

endsWith()วิธีการนี้คำนึงถึงขนาดตัวพิมพ์


รองรับเบราว์เซอร์

endsWith()เป็นคุณลักษณะ ES6 (JavaScript 2015)

ได้รับการสนับสนุนในเบราว์เซอร์ที่ทันสมัยทั้งหมด:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

endsWith()ไม่รองรับใน Internet Explorer


อ้างอิงสตริงที่สมบูรณ์

สำหรับการอ้างอิงสตริงที่สมบูรณ์ ไปที่:

ทำการอ้างอิงสตริง JavaScriptให้สมบูรณ์

ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างของคุณสมบัติสตริงและวิธีการทั้งหมด