개발을 하다보면 특정 문자열까지 잘라서 쓰고 싶은 경우가 생긴다.


방식은 간단하다.


일단 indexOf 함수를 이용하여 위치를 찾자.


그런 후 시작부터 해당위치까지 substr를 이용해서 구하면 된다.


이를 응용하여 특정 문자열 구간을 자를수도 있다.


var testStr = "손흥민(토트넘)";
var groupIndex = 0;

if(testStr.indexOf("(")!=-1){
groupIndex = testStr.indexOf("(");
console.log(testStr);
console.log(testStr.substr(0,groupIndex)) ;
}


첫번째 결과값으로는 손흥민(토트넘)


두번째 결과값으로는 손흥민이 나오게 된다.

+ Recent posts