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ให้สมบูรณ์
ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างของคุณสมบัติสตริงและวิธีการทั้งหมด