Published 2022. 11. 10. 11:55

JavaScript Search Methods

  • String indexOf()
  • String lastIndexOf()
  • String search()
  • String match()
  • String matchAll()
  • String includes()
  • String startsWith()
  • String endsWith()

 

indexOf()

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

 

 

lastIndexOf()

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

 

 

indexOf(), lastIndexOf()텍스트가 없으면 -1을 반환합니다.

 

검색의 시작 위치로 두 번째 매개변수를 허용합니다.

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

 

 

 

match()

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

 

 

 

matchAll()

const iterator = text.matchAll("Cats");

 

 

 

includes()

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

 

 

 

 

startsWith()

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

 

 

 

endsWith()

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

'JavaScript' 카테고리의 다른 글

JavaScript - Number Methods  (0) 2022.11.15
JavaScript - Template Literals  (0) 2022.11.10
JavaScript - String  (0) 2022.11.10
JavaScript - Object  (0) 2022.11.10
JavaScript - Output  (0) 2022.11.08
복사했습니다!