답변:
내가 이것을하는 방법 :
// function you can use:
function getSecondPart(str) {
return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
get everything after the dash in a string in javascript이 경우에 실패 할 것이라는 질문이 있었다 .
내가 선호하는 솔루션은 다음과 같습니다.
const str = 'sometext-20202';
const slug = str.split('-').pop();
slug당신의 결과는 어디에 있습니까
AFAIK는 모두 substring()와 indexOf()모질라와 IE에서 모두 지원된다. 그러나 일부 브라우저의 이전 버전 (예 : Netscape / Opera)에서는 substr ()이 지원되지 않을 수 있습니다.
귀하의 게시물에 substring()및 사용 방법을 이미 알고 있음을 나타내 indexOf()므로 코드 샘플을 게시하지 않습니다.
myString.split('-').splice(1).join('-')
myString.split('-').splice(-1).join('-')
sometext-20202-303있습니까?