Az indexOf()
metódus az indexet (pozíció) adja vissza. egy karakterlánc első előfordulása egy karakterláncban:
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>The indexOf() method returns the position of the first occurrence of a string in a string.</p>
<p>The position of the first occurrence of "locate" is:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
A JavaScript nullától számítja a pozíciókat.
A 0 az első pozíció a-ban karakterlánc, 1 a második, 2 a harmadik, ...
A lastIndexOf()
metódus az utolsó indexét adja vissza. megadott szöveg előfordulása egy karakterláncban:
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The lastIndexOf() Method</h2>
<p>The position of the last occurrence of "locate" is:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
Az indexOf()
és a lastIndexOf()
is -1-et ad vissza ha a szöveg nem található:
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("John");
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>Both indexOf() and lastIndexOf() return -1 if the text is not found:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("John");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
Mindkét módszer elfogad egy második paramétert a kiindulási helyzetként keresés:
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate", 15);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>The indexOf() method accepts a second parameter as the starting position for the search:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate",15);
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
A lastIndexOf()
metódus visszafelé keres (a végétől az elejéig), jelentése: ha a második paraméter 15
, a keresés a pozíciónál kezdődik 15, és a karakterlánc elejéig keres.
let text = "Please locate where 'locate' occurs!";
text.lastIndexOf("locate", 15);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The lastIndexOf() Method</h2>
<p>The lastIndexOf() method accepts a second parameter as the starting position for the search.</p>
<p>Remember that the lastIndexOf() method searches backwards, so position 15 means start the search at position 15, and search to the beginning.</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate", 15);
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
A search()
metódus egy karakterláncban keres karakterláncot (vagy reguláris kifejezést) és visszaadja a meccs pozícióját:
let text = "Please locate where 'locate' occurs!";
text.search("locate");
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The search() Method</h2>
<p>The search() method returns the position of the first occurrence of a string in a string.</p>
<p>The position of the first occurrence of "locate" is:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.search("locate");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
let text = "Please locate where 'locate' occurs!";
text.search(/locate/);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The search() Method</h2>
<p>The search() method returns the position of the first occurrence of a string in a string.</p>
<p>Return the position of the first occurrence of a regular expression:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.search(/locate/);
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
A két metódus, az indexOf()
és a search()
egyenlő?
Ugyanazokat az argumentumokat (paramétereket) fogadják el, és ugyanazt az értéket adják vissza?
A két módszer NEM egyenlő. Ezek a különbségek:
A search()
metódus nem vehet fel második kezdőpozíció argumentumot.
Az indexOf()
metódus nem fogadható el erőteljes keresési értékek (reguláris kifejezések).
Többet megtudhat róla reguláris kifejezéseket egy későbbi fejezetben.
A match()
metódus az egyezés eredményeit tartalmazó tömböt ad vissza egy karakterlánc karakterlánchoz (vagy reguláris kifejezéshez) képest.
Végezzen keresést az "ain" kifejezésre:
let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The match() Method</h2>
<p>Perform a search for "ain":</p>
<p id="demo"></p>
<script>
let text = "The rain in SPAIN stays mainly in the plain";
const myArr = text.match("ain");
document.getElementById("demo").innerHTML = myArr.length + " " + myArr;
</script>
</body>
</html>
Végezzen keresést az "ain" kifejezésre:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The match() Method</h2>
<p>Perform a search for "ain":</p>
<p id="demo"></p>
<script>
let text = "The rain in SPAIN stays mainly in the plain";
const myArr = text.match(/ain/);
document.getElementById("demo").innerHTML = myArr.length + " " + myArr;
</script>
</body>
</html>
Végezzen globális keresést az "ain" kifejezésre:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The match() Method</h2>
<p>Perform a global search for "ain":</p>
<p id="demo"></p>
<script>
let text = "The rain in SPAIN stays mainly in the plain";
const myArr = text.match(/ain/g);
document.getElementById("demo").innerHTML = myArr.length + " " + myArr;
</script>
</body>
</html>
Végezzen globális, a kis- és nagybetűket figyelmen kívül hagyó keresést az "ain" kifejezésre:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The match() Method</h2>
<p>Perform a global, case-insensitive search for "ain":</p>
<p id="demo"></p>
<script>
let text = "The rain in SPAIN stays mainly in the plain";
const myArr = text.match(/ain/gi);
document.getElementById("demo").innerHTML = myArr.length + " " + myArr;
</script>
</body>
</html>
Ha egy reguláris kifejezés nem tartalmazza a g módosítót (globális keresés), A match()
csak a karakterlánc első egyezését adja vissza.
A reguláris kifejezésekről a JS RegExp című fejezetben olvashat bővebben.
A matchAll()
metódus az egyezés eredményeit tartalmazó iterátort ad vissza egy karakterlánc karakterlánchoz (vagy reguláris kifejezéshez) képest.
const iterator = text.matchAll("Cats");
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll("Cats");
document.getElementById("demo").innerHTML = Array.from(iterator);
</script>
</body>
</html>
Ha a paraméter reguláris kifejezés, akkor a globális jelzőt (g) kell beállítani, ellenkező esetben TypeError jelenik meg.
const iterator = text.matchAll(/Cats/g);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll(/Cats/g);
document.getElementById("demo").innerHTML = Array.from(iterator);
</script>
</body>
</html>
Ha a kis- és nagybetűk között nem szeretne keresni, akkor az érzéketlen jelzőt (i) be kell állítani:
const iterator = text.matchAll(/Cats/gi);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll(/Cats/gi);
document.getElementById("demo").innerHTML = Array.from(iterator);
</script>
</body>
</html>
A matchAll()
egy ES2020 szolgáltatás.
A matchAll()
nem működik az Internet Explorer böngészőben.
Az includes()
metódus igazat ad vissza, ha egy karakterlánc megadott értéket tartalmaz.
Ellenkező esetben a false
értéket adja vissza.
Ellenőrizze, hogy egy karakterlánc tartalmazza-e a "világ" szót:
let text = "Hello world, welcome to the universe.";
text.includes("world");
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The includes() Method</h2>
<p>Check if a string includes "world":</p>
<p id="demo"></p>
<p>The includes() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.includes("world");
</script>
</body>
</html>
Ellenőrizze, hogy egy karakterlánc tartalmazza-e a "világ" szót. Kezdje a 12-es pozícióból:
let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The includes() Method</h2>
<p>Check if a string includes "world" starting from position 12:</p>
<p id="demo"></p>
<p>The includes() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.includes("world", 12);
</script>
</body>
</html>
Az includes()
megkülönbözteti a kis- és nagybetűket.
Az includes()
egy ES6 szolgáltatás.
Az includes()
nem támogatott az Internet Explorer böngészőben.
A startsWith()
metódus true
értékkel tér vissza ha egy karakterlánc egy megadott értékkel kezdődik.
Ellenkező esetben a false
értéket adja vissza:
Igazat ad vissza:
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p>Check if a string starts with "Hello":</p>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("Hello");
</script>
</body>
</html>
False értéket ad vissza:
let text = "Hello world, welcome to the universe.";
text.startsWith("world")
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("world");
</script>
</body>
</html>
A keresés kezdőpontja megadható:
False értéket ad vissza:
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5)
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("world", 5);
</script>
</body>
</html>
Igazat ad vissza:
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6)
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("world", 6);
</script>
</body>
</html>
A startsWith()
megkülönbözteti a kis- és nagybetűket.
A startsWith()
egy ES6 szolgáltatás.
A startsWith()
nem támogatott az Internet Explorer böngészőben.
Az endsWith()
metódus true
értékkel tér vissza ha egy karakterlánc meghatározott értékkel végződik.
Ellenkező esetben a false
értéket adja vissza:
Ellenőrizze, hogy egy karakterlánc „Doe”-re végződik-e:
let text = "John Doe";
text.endsWith("Doe");
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Check if a string ends with "Doe":</p>
<p id="demo"></p>
<p>The endsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "John Doe";
document.getElementById("demo").innerHTML = text.endsWith("Doe");
</script>
</body>
</html>
Ellenőrizze, hogy egy karakterlánc első 11 karaktere "világ"-ra végződik-e:
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Check in the 11 first characters of a string ends with "world":</p>
<p id="demo"></p>
<p>The endsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.endsWith("world", 11);
</script>
</body>
</html>
Az endsWith()
megkülönbözteti a kis- és nagybetűket.
Az endsWith()
egy ES6 szolgáltatás.
Az endsWith()
nem támogatott az Internet Explorer böngészőben.
A teljes karakterlánc-referenciáért látogasson el ide:
Teljes JavaScript karakterlánc hivatkozás.
A hivatkozás leírásokat és példákat tartalmaz az összes karakterlánc tulajdonságra és metódusra.