πŸ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • μ½œλ°±ν•¨μˆ˜ (Callback function)
    • ν•¨μˆ˜μ˜ 호좜
    • 자료ꡬ쑰 new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • 객체
    • Arguments
    • higher order function
    • operators-function-control-flow
    • 객체 생성 νŒ¨ν„΄ 3κ°€μ§€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • 데이터 νƒ€μž…
    • Object 객체
    • ν‘œμ€€ λ‚΄μž₯ 객체의 ν™•μž₯
    • μ°Έμ‘°
    • ν•¨μˆ˜
    • 상속(Inheritance)
    • this - 'this'λ₯Ό μ‚¬μš©ν•˜λŠ” ν•΄λ‹Ή ν•¨μˆ˜λ₯Ό 'μ–΄λ–»κ²Œ' μ‹€ν–‰ν•˜λŠλƒμ— λ”°λ₯Έ 4κ°€μ§€ this μ •μ˜
    • 전역객체(Global object)
    • 객체 μ§€ν–₯ ν”„λ‘œκ·Έλž˜λ°
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - μžμ—°μˆ˜, μ •μˆ˜, 10μ§„μˆ˜, 2μ§„μˆ˜, λΆ€λ™μ†Œμˆ˜μ , μ‹€μˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈμ™€ 클래슀, μ–΄λ–€ 차이가 μ‘΄μž¬ν• κΉŒ?
    • Hooksκ³Ό useEffect μ‚¬μš©ν•΄ 보기
    • Route
    • async wait μ‚¬μš©ν•˜κΈ°
    • Hooks API Reference
    • context
    • npm uninstall ν•˜λŠ”λ²•
    • test λ§Œλ“€κΈ°
  • tip
    • ν΄λ¦°μ½”λ“œ
    • BxSlider둜 ν…μŠ€νŠΈ 흐λ₯΄λŠ” 효과 λ§Œλ“€κΈ°
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • μš°μ•„ν•œ νƒ€μž…μŠ€ν¬λ¦½νŠΈ 2λΆ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • μš°μ•„ν•œ νƒ€μž…μŠ€ν¬λ¦½νŠΈ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • λ¨Έμ§€ ν›„ branch μ‚­μ œν•˜κΈ°
    • μ§€μ˜₯μ—μ„œ 온 Git
    • reset-revert
    • develop branch λ₯Ό pull ν•˜κ³  μ‹Άμ„λ•Œ
  • conference-and-seminar
    • λͺ¨λ˜ ν”„λ‘ νŠΈμ—”λ“œ κ°œλ°œν™˜κ²½μ˜ 이해
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • μ•Œκ³ λ¦¬μ¦˜ μ½”λ“œλ¦¬λ·°
    • 슀슀둜 κ³΅λΆ€ν•˜λŠ” 법
    • 바닐라코딩 μˆ˜κ°• ν›„κΈ°
    • async 과제 ν›„κΈ° - 비동기, 동기, ν΄λ‘œμ €, λ°°μ—΄κ³Ό 객체
    • μœ μš©ν•œ μ‚¬μ΄νŠΈ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars 이용자 μ†”λ£¨μ…˜ λͺ¨μŒ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • νƒ€μž…κ³Ό 좔상화
      • 객체 지도
      • μ΄μƒν•œ λ‚˜λΌμ˜ 객체
      • 좔상화 기법
      • 05. μ±…μž„κ³Ό λ©”μ‹œμ§€
      • 07.ν•¨κ»˜ λͺ¨μœΌκΈ°
      • 04. μ—­ν• , μ±…μž„, ν˜‘λ ₯
      • ν˜‘λ ₯ν•˜λŠ” κ°μ²΄λ“€μ˜ 곡동체
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • μ›Ή λΈŒλΌμš°μ €μ— URL을 μž…λ ₯ν–ˆμ„ λ•Œ μ–΄λ–»κ²Œ μ›Ή νŽ˜μ΄μ§€κ°€ λ³΄μ—¬μ§ˆκΉŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex 해버렸지 뭐야
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd λͺ…λ Ήμ–΄ λͺ¨μŒ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN μ„€μ •ν•˜κΈ°
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans μ˜€λ‹΅λ…ΈνŠΈ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • μ½”λ“œ μŠ€ν”ΌμΈ  - CSS Rendering 1회차 2/2
    • μ½”λ“œ μŠ€ν”ΌμΈ  - CSS Rendering 1회차 1/2
  • you-dont-know-js
    • νƒ€μž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • Syntax
  • Parameters
  • Return value
  • Description
  • λ§€κ°œλ³€μˆ˜κ°€ λ¬Έμžμ—΄λ‘œ μ§€μ •λ˜μ—ˆμ„ λ•Œ
  • ν•¨μˆ˜λ₯Ό 맀개 λ³€μˆ˜λ‘œ μ§€μ •
  • Examples
  • replace()의 μ •κ·œν‘œν˜„μ‹ μ •μ˜
  • globalκ³Ό ignore을 replace()μ—μ„œ μ‚¬μš©ν•˜κΈ°
  • λ¬Έμžμ—΄μ—μ„œ 단어 μΉ˜ν™˜
  • μΌμΉ˜ν•˜λŠ” 문자λ₯Ό μˆ˜μ •ν•˜λŠ” 인라인 ν•¨μˆ˜ μ‚¬μš©
  • 화씨λ₯Ό 섭씨 μ˜¨λ„λ‘œ λŒ€μ²΄

Was this helpful?

  1. javascript-api

string.replace()

replace() λ©”μ„œλ“œλŠ” μ–΄λ–€ pattern에 μΌμΉ˜ν•˜λŠ” 일뢀 λ˜λŠ” λͺ¨λ“  뢀뢄을 replacement둜 λŒ€μ²΄ν•˜μ—¬ μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•œλ‹€. κ·Έ pattern은 λ¬Έμžμ—΄μ΄λ‚˜ μ •κ·œμ‹(RegExp)이 될 수 있으며 replacementλŠ” λ¬Έμžμ—΄μ΄λ‚˜ 각 λ§€μΉ˜μ— λŒ€ν•΄μ„œ 호좜된 ν•¨μˆ˜μΌ 수 μžˆλ‹€. pattern 이 λ¬Έμžμ—΄μΌ 경우, 졜초둜 μΌμΉ˜ν•œ λΆ€λΆ„λ§Œ λŒ€μ²΄λœλ‹€.

const p = 'Cats are the best! cats are cute!';

const regex = /cats/gi; // iλŠ” λŒ€μ†Œλ¬Έμžλ₯Ό λ”°μ§€μ§€ μ•ŠλŠ”λ‹€.

p.replace(regex, 'Dogs'); // "Dogs are the best! Dogs are cute!"
p.replace(regex, 'Hamsters'); // "Hamsters are the best! Hamsters are cute!"

Syntax

const newStr = str.replace(regexp\substr, newSubstr\function);

Parameters

regexp (pattern)

RegExp μ •κ·œμ‹ 객체 λ˜λŠ” λ¦¬ν„°λŸ΄

μΌμΉ˜ν•˜λŠ” ν•­λͺ©μ€ newSubStr λ˜λŠ” μ§€μ •λœ ν•¨μˆ˜κ°€ λ°˜ν™˜ ν•œ κ°’μœΌλ‘œ λŒ€μ²΄λœλ‹€.

substr (pattern - λ¬Έμžμ—΄μΈ 경우)

newSubStr 둜 λŒ€μ²΄ 될 String, μ •κ·œμ‹μœΌλ‘œ ν•΄μ„ν•˜μ§€ μ•Šκ³  λ¨Όμ € λ‚˜μ˜¨ κ²ƒλ§Œ 바뀐닀.

newSubStr (replacement)

λ§€κ°œλ³€μˆ˜λ‘œ μ§€μ •λœ regexp(pattern) λ‚˜ substr (pattern) λ¬Έμžμ—΄μ„ λŒ€μ²΄ν•˜λŠ” 문자, μ—¬λŸ¬κ°€μ§€ 특수 replacement pattern이 μ§€μ›λ˜λ©°, "λ§€κ°œλ³€μˆ˜κ°€ string으둜 μ§€μ •" μ°Έκ³ 

function (replacement)

μ£Όμ–΄μ§„ regexp λ˜λŠ” substr둜 일치 ν•­λͺ©μ„ λŒ€μ²΄ν•˜λŠ”λ° μ‚¬μš©ν•  μƒˆ ν•˜μœ„ λ¬Έμžμ—΄μ„ λ§Œλ“€κΈ° μœ„ν•΄ ν˜ΈμΆœλ˜λŠ” ν•¨μˆ˜

이 ν•¨μˆ˜μ— 제곡된 μΈμˆ˜λŠ” "ν•¨μˆ˜ λ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ μ§€μ •"μ°Έκ³ 

Return value

일뢀 λ˜λŠ” 전체 νŒ¨ν„΄μ΄ λŒ€μ²΄λœ μƒˆ λ¬Έμžμ—΄

Description

이 λ©”μ„œλ“œλŠ” 호좜된 String obejctλ₯Ό λ°”κΎΈμ§€ μ•ŠλŠ”λ‹€. μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•œλ‹€.

전체λ₯Ό κ²€μƒ‰ν•˜κ³  λ³€κ²½ν•˜λ©΄ g ν”Œλž˜κ·Έλ₯Ό μ‚¬μš©ν•œλ‹€.

λ§€κ°œλ³€μˆ˜κ°€ λ¬Έμžμ—΄λ‘œ μ§€μ •λ˜μ—ˆμ„ λ•Œ

λŒ€μ²΄ λ¬Έμžμ—΄μ€ λ‹€μŒκ³Ό 같은 특수 ꡐ체 νŒ¨ν„΄μ„ 포함할 수 μžˆλ‹€.

νŒ¨ν„΄

μ‚½μž…

$$

"$" μ‚½μž…

$&

맀치된 λΆ€λΆ„ λ¬Έμžμ—΄μ„ μ‚½μž…

$`

맀치된 λΆ€λΆ„ λ¬Έμžμ—΄ μ•žμ˜ 단어 뢀뢄을 μ‚½μž…ν•œλ‹€.

$'

맀치된 λΆ€λΆ„ λ¬Έμžμ—΄ μ§ν›„μ˜ λ¬Έμžμ—΄ 뢀뢄을 μ‚½μž…

$n

n이 1이상 99μ΄ν•˜μ˜ μ •μˆ˜λΌλ©΄, 첫번째 λ§€κ°œλ³€μˆ˜λ‘œ λ„˜κ²¨μ§„ RegExp κ°μ²΄μ—μ„œ μ†Œκ΄„ν˜Έλ‘œ 묢인 n 번째의 λΆ€λΆ„ ν‘œν˜„μ‹μœΌλ‘œ 맀치된 λ¬Έμžμ—΄μ„ μ‚½μž…ν•œλ‹€. μΈλ±μŠ€κ°€ 1λΆ€ν„° μ‹œμž‘λœλ‹€.

$& μ˜ˆμ‹œ

const html = `<ul>
    <li>고양이</li>
    <li>κ°•μ•„μ§€</li>
    <li>고양이</li>
</ul>`;
const regex = /고양이/gi;

const newHtml = html.replace(regex, "<a href='#none'>$&</a>");
/*
"<ul>
    <li><a href='#none'>고양이</a></li>
    <li>κ°•μ•„μ§€</li>
    <li><a href='#none'>고양이</a></li>
</ul>"
*/

$`

const str = '고양이1 κ°•μ•„μ§€1 고양이2 κ°•μ•„μ§€2 고양이3';
const regex =  /κ°•μ•„μ§€/gi;
const result = str.replace(regex, "$`μ•žμ— μžˆλŠ” ν…μŠ€νŠΈκ°€ λ³΅μ‚¬λœλ‹€"); // ν˜„μž¬ ν…μŠ€νŠΈλŠ” μ‚­μ œλœλ‹€. 
// "고양이1 고양이1 μ•žμ— μžˆλŠ” ν…μŠ€νŠΈκ°€ λ³΅μ‚¬λœλ‹€1 고양이2 고양이1 κ°•μ•„μ§€1 고양이2 μ•žμ— μžˆλŠ” ν…μŠ€νŠΈκ°€ λ³΅μ‚¬λœλ‹€2 고양이3"

$'

const str = '고양이1 κ°•μ•„μ§€1 고양이2 κ°•μ•„μ§€2 고양이3';
const regex =  /κ°•μ•„μ§€/gi;
const result = str.replace(regex, "$'뒀에 있던 ν…μŠ€νŠΈκ°€ λΆ™λŠ”λ‹€.");
// "고양이1 1 고양이2 κ°•μ•„μ§€2 고양이3뒀에 있던 ν…μŠ€νŠΈκ°€ λΆ™λŠ”λ‹€.1 고양이2 2 고양이3뒀에 있던 ν…μŠ€νŠΈκ°€ λΆ™λŠ”λ‹€.2 고양이3"

ν•¨μˆ˜λ₯Ό 맀개 λ³€μˆ˜λ‘œ μ§€μ •

λ‘λ²ˆμ§Έ λ§€κ°œλ³€μˆ˜κ°€ ν•¨μˆ˜λ‘œ 지정될 수 μžˆλ‹€. μΌμΉ˜ν•˜λŠ” 경우 ν•¨μˆ˜κ°€ ν˜ΈμΆœλœλ‹€. ν•¨μˆ˜μ˜ κ²°κ³Ό(λ°˜ν™˜κ°’)κ°€ λŒ€μ²΄ λ¬Έμžμ—΄λ‘œ μ‚¬μš©λœλ‹€.

(μ°Έκ³ : 이 경우 μœ„μ— μ–ΈκΈ‰ 된 νŠΉμΆ” ꡐ체 νŒ¨ν„΄μ€ μ μš©λ˜μ§€ μ•ŠλŠ”λ‹€.)

λ§€κ°œλ³€μˆ˜ πŸ‘‡

이름

μ£Όμ–΄μ§„ κ°’

match

맀치된 λ¬Έμžμ—΄(μœ„μ˜ $& 에 ν•΄λ‹Ή)

p1, p2, ...

replace() 첫번째 μΈμˆ˜κ°€ RegExp 객체인 경우, n번째 κ΄„ν˜Έμ—μ„œ 갭쳐된 그룹의 λ¬Έμžμ—΄($1, $2에 λŒ€μ‘)이닀. 예λ₯Ό λ“€μ–΄ /(\a+)(\b+)/ 이 μ£Όμ–΄μ§„λ‹€λ©΄ p1 은 \a+ κ³Ό 맀치되고 p2λŠ” \b+ κ³Ό λ§€μΉ˜λœλ‹€.

offset

검색쀑인 전체 λ¬Έμžμ—΄ μ€‘μ—μ„œ μΌμΉ˜ν•˜λŠ” λΆ€λΆ„ λ¬Έμžμ—΄μ˜ μ˜€ν”„μ…‹μ΄λ‹€. (예λ₯Ό λ“€μ–΄, 전체 λ¬Έμžμ—΄μ΄ 'abcd'이고 μΌμΉ˜ν•˜λŠ” ν•˜μœ„ λ¬Έμžμ—΄μ€ 'bc'인 경우 μΈμˆ˜λŠ” 1이닀.)

string

전체 λ¬Έμžμ—΄μ„ κ²€μ‚¬ν•œλ‹€.

인수의 μ •ν™•ν•œ κ°œμˆ˜λŠ” 첫번째 μΈμˆ˜κ°€ RegExp객체인지 여뢀에 따라 달라지며, κ΄„ν˜Έλ‘œ μ§€μ •λœ λΆ€λΆ„μ˜ κ°œμˆ˜μ— 따라 달라진닀.

λ‹€μŒ μ˜ˆμ œλŠ” newString 을 abc-12345-#$*% 둜 κ΅μ²΄ν•œλ‹€.

function replacer(match, p1, p2, p3, offset, string) {
  console.log('offset', offset); // offset 0 검색 쀑인 전체 λ¬Έμžμ—΄μ€‘μ— μΌμΉ˜κ°€ μ‹œμž‘λ˜λŠ” λΆ€λΆ„
  console.log('string', string); // string abc12345#$*%
  // p1λŠ” μˆ«μžκ°€ μ•„λ‹Œ 것, p2λŠ” 숫자, p3λŠ” μˆ«μžμ™€ λ¬Έμžκ°€ μ•„λ‹Œ 것 
  return [p1, p2, p3].join(' - '); // abc - 12345 - #$*%
}

let newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);

Examples

replace()의 μ •κ·œν‘œν˜„μ‹ μ •μ˜

iλŠ” λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λΆ„ν•˜μ§€ μ•ŠλŠ”λ‹€.

const str = 'Cats are the best! cats are cute!';
const newStr = str.replace(/cats/i, 'dogs');
newStr; // "dogs are the best! cats are cute!"

globalκ³Ό ignore을 replace()μ—μ„œ μ‚¬μš©ν•˜κΈ°

global replaceλŠ” μ •κ·œμ‹μ—μ„œλ§Œ μ‚¬μš©ν•  수 μžˆλ‹€. μ „μ—­μœΌλ‘œ 탐색할 수 μžˆλ‹€.

const str = 'Cats are the best! cats are cute!';
const newStr = str.replace(/cats/gi, 'dogs');
newStr; // "dogs are the best! dogs are cute!"

λ¬Έμžμ—΄μ—μ„œ 단어 μΉ˜ν™˜

λŒ€μ²΄ ν…μŠ€νŠΈμ˜ 경우 μŠ€ν¬λ¦½νŠΈλŠ” $1, $2와 그룹캑쳐λ₯Ό μ‚¬μš©ν•œλ‹€.

const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const result = str.replace(re, '$2, $1');

result; // "Smith, John"

μΌμΉ˜ν•˜λŠ” 문자λ₯Ό μˆ˜μ •ν•˜λŠ” 인라인 ν•¨μˆ˜ μ‚¬μš©

이 μ˜ˆμ‹œμ—μ„œλŠ” λͺ¨λ“  λŒ€λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ λ³€ν™˜λ˜κ³ , μΌμΉ˜ν•˜λŠ” μœ„μΉ˜ λ°”λ‘œ μ•žμ— ν•˜μ΄ν”ˆμ΄ μ‚½μž…λœλ‹€.

μ—¬κΈ°μ„œ μ€‘μš”ν•œκ²ƒμ€ μΆ”κ°€ μž‘μ—…μ€ μΌμΉ˜ν•˜λŠ” ν•­λͺ©μ΄ λŒ€μ²΄λ˜μ–΄ λ°˜ν™˜ν•˜κΈ° 전에 ν•„μš”ν•œ 것이닀.

λŒ€μ²΄ ν•¨μˆ˜μ—μ„œλŠ” match된 뢀뢄이 ν•¨μˆ˜μ˜ 인자둜 λ“€μ–΄μ˜¨λ‹€. 그리고 κ·Έ 인수λ₯Ό λŒ€μ†Œλ¬Έμž λ³€ν˜• λ°˜ν™˜ 직전에 ν•˜μ΄ν”ˆμœΌλ‘œ μ—°κ²°λœλ‹€.

function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset > 0 ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}

styleHyphenFormat('borderTop'); // "border-top"

μ΅œμ’… λŒ€μ²΄κ°€ 이루어지기 전에 μΌμΉ˜ν•˜λŠ” κ²°κ³Όλ₯Ό μΆ”κ°€λ‘œ λ³€ν™˜ν•˜λ €λŠ” ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€. toLowerCase() λ©”μ„œλ“œ 전에 ν‰κ°€λ˜μ–΄μ•Ό ν•œλ‹€. ν•¨μˆ˜μ—†μ΄ λ§€μΉ˜μ— μ‚¬μš©ν•˜λŠ” 경우 toLowerCase() 방법은 νš¨κ³Όκ°€ 없을 것이닀.

// '$&: 맀치된 λΆ€λΆ„ λ¬Έμžμ—΄μ„ μ‚½μž…'
const newString = 'borderTop'.replace(/[A-Z]/, '-'+ '$&'.toLowerCase()); 
// "border-Top" μ†Œλ¬Έμžλ‘œ λ³€ν™˜λ˜μ§€ μ•Šμ•˜λ‹€.

문자λ₯Ό νŒ¨ν„΄μœΌλ‘œ μ‚¬μš©ν•˜κΈ° 전에 λ¨Όμ € '$&'.toLowerCase() λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄λ‘œ ν‰κ°€λ˜κΈ° λ•Œλ¬Έμ΄λ‹€.

화씨λ₯Ό 섭씨 μ˜¨λ„λ‘œ λŒ€μ²΄

화씨 μ˜¨λ„ F둜 λλ‚˜λŠ” μˆ«μžμ΄μ–΄μ•Ό ν•œλ‹€. ν•¨μˆ˜λŠ” C둜 λλ‚˜λŠ” μ„­μ”¨λ‘œ λŒλ €μ€€λ‹€. 예λ₯Ό λ“€μ–΄, μž…λ ₯λ˜λŠ” μˆ˜κ°€ 212F인 경우, ν•¨μˆ˜λŠ” 100Cλ₯Ό λŒλ €μ€€λ‹€. μž…λ ₯λ˜λŠ” 숫자 0F인 경우, ν•¨μˆ˜λŠ” -17.77777777777778Cλ₯Ό λŒλ €μ€€λ‹€.

μ •κ·œ ν‘œν˜„μ‹ testλŠ” μž„μ˜μ˜ μˆ«μžκ°€ F둜 λλ‚˜λŠ”μ§€ ν™•μΈν•œλ‹€. 화씨 μ˜¨λ„μ˜ μˆ˜λŠ” ν•¨μˆ˜μ˜ λ‘λ²ˆμ§Έ 인수 p1을 톡해 κ·Έ κΈ°λŠ₯에 μ•‘μ„ΈμŠ€ ν•  수 μžˆλ‹€. f2c()ν•¨μˆ˜λŠ” λ¬Έμžμ—΄μ„ 전달받아 화씨λ₯Ό κΈ°μ€€μœΌλ‘œ 섭씨 숫자λ₯Ό μ„€μ •ν•œλ‹€. κ·ΈλŸ°λ‹€μŒ f2c() ν•¨μˆ˜λŠ” 섭씨 숫자λ₯Ό λ°˜ν™˜ν•œλ‹€. 이 ν•¨μˆ˜λŠ” Prel의 s///e ν•¨μˆ˜μ™€ μœ μ‚¬ν•˜λ‹€.

function f2c(x) {
  function convert(str, p1, offset, s) {
    return ((p1 - 32) * 5/9) + 'C';
  }
  let s = String(x);
  let test = /(-?\d+(?:\.\d*)?)F\b/g;
  return s.replace(test, convert);
}

f2c("212F"); // "100C"
Previousstring.includesNextstring.charAt

Last updated 5 years ago

Was this helpful?