정규식으로 좌표꺼내기

(37.11111111111111, 126.11111111111111)

  • 이런 좌표를 나타내는 문자열에서 알맹이만 빼보자
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let coordString = '(37.11111111111111, 126.11111111111111)'
/\d+([.]\d+)?/.exec(coordString)
// "37.11111111111111,.11111111111111"

function getPositionFromString(positionString) {
const re = /(\d+)([\.]\d+)?/g;
const result = []

while(match = re.exec(positionString)){
// console.info(match[0], ' found at : ', match.index);
result.push(match[0])
}

// console.info({x:result[0],y:result[1]})
return {x:result[0],y:result[1]}
}
  • exec() 가 g옵션에 따라서 매치하는 패턴의 결과가 모두 담겨서 나오는줄 알았다
  • 위처럼 while 루프를 통해서 모든 매치결과를 받아올 수 있다
  • 내부적으로 커서가 있는 것 같다

참고

Author

chinsung

Posted on

2020-11-24

Updated on

2021-08-04

Licensed under

댓글