빠른 참조

Underscore.string 치트시트

JavaScript 문자열 조작 라이브러리 underscore.string 빠른 참조 가이드: 대소문자 변환, 패딩, 트림, 새니타이즈 및 숫자 서식 지정.

· 게시일

underscore.string은 JavaScript를 위한 다양한 문자열 조작 확장 기능을 제공합니다.

대소문자 및 표기법 변환

s.capitalize("foo bar")       //=> "Foo bar"
s.humanize("hey-there foo")   //=> "Hey there foo"
s.titleize('my name is hi')   //=> "My Name Is Hi"

s.dasherize('MozTransform')   //=> "-moz-transform"
s.underscored('MozTransform') //=> "moz_transform"
s.classify('-moz-transform')  //=> "MozTransform"
s.camelize('moz_transform')   //=> "mozTransform"

s.slugify("hey there")        //=> "hey-there"
s.swapCase("hELLO")           //=> "Hello"

문자열 검사 및 확인

s.startsWith('image.gif', 'image') //=> true
s.endsWith('image.gif', '.gif')    //=> true
s.isBlank("   ")                   //=> true

HTML 엔티티 및 태그 제거

s.escapeHTML("<div>")       //=> "&lt;div&gt;"
s.unescapeHTML("&lt;div&gt;") //=> "<div>"
s.stripTags("<div>hi</div>")  //=> "hi"

따옴표 처리 및 분할

s.quote("hi", '"')    //=> '"hi"'
s.unquote('"hi"')     //=> "hi"

s.lines("hi\nthere")   //=> ["hi", "there"]
s.words("hi  there")  //=> ["hi", "there"]

패딩 및 숫자 서식 지정

s.pad("1", 8)               //=> "       1"
s.pad("1", 8, "0")          //=> "00000001"
s.pad("1", 8, " ", "right") //=> "1       "
s.pad("1", 8, " ", "both")  //=> "    1   "

s.numberFormat(1000, 2)     //=> "1,000.00"
s.sprintf("%.1f", 1.17)     //=> "1.2"

참고자료

관련 주제