The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it.
‘too’ 文字列を検索する -> /too/ The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it.
大、小文字を構わなく ‘t’ 文字を検索するとき -> /t/-gi The greatest danger for most of us is notthat our aim is too high and we miss it, butthat it is too low and we reach it.
‘d’ から始まる単語及び文字のすべてを検索するとき -> /[d]\w+/-gi The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it.
‘ea’を含む単語を検索するとき -> /[a-z]*ea[a-z]*/-i The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it
If you see ‘echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list‘, move forward.
substring 함수도 slice와 마찬가지로 위치(인덱스)를 매개변수로 전달하여 원하는 문자열을 추출합니다.
기본적인 작동 방식은 같으나 slice와 달리 시작인덱스가 종료인덱스보다 큰 경우에는 자동으로 두 값의 위치를 변경하여 함수를 실행합니다.
또한 substring은 매개변수에 음수를 넣으면 이 값을 0으로 인식합니다.
따라서 (3, -1)을 전달하게 되면 (3, 0)으로 인식합니다. (3, 0)은 시작 인덱스가 더 크므로 위치를 변환하여 (0, 3)의 값을 계산한 결과를 반환합니다.
함수명이 비슷한 substr의 경우 ‘위치’와 ‘추출할 문자의 수’를 파라미터로 전달하는 함수입니다.
const myWord = 'invierno';
myWord.substring(3); // 'ierno'
myWord.substring(1,3); // 'nv'
myWord.substring(3,1); // 'nv' -> 시작이 종료인덱스보다 크면 자동으로 위치 변경 후 함수를 실행
myWord.substring(3,-1); // 'inv' -> 음수는 0, 시작인덱스와 종료인덱스는 위치 변경 후 계산
myWord.substring(-4); // 'invierno' -> 음수는 0, 시작인덱스만 전달하여 0부터 끝까지 계산
myWord.substr(1,3); // 'nvi' -> 시작인덱스 1부터 문자 3개를 추출
const mySentence = 'my name is:Ron';
mySentence.replace(':',' '); //'my name is Ron'
const mySentence = 'my_name_is_Ron';
mySentence.replace('_',' '); // 'my name_is_Ron'
//정규표현식 사용해서 모든 문자 변경하기
mySentence.replace(/_/g, ' '); // 'my name is Ron'
이 개념은 미국의 수학자인 Stephen Cole Kleene가 1950년대에 정의한 정규 언어(Regular Language)와 관련이 있으며, 정규표현식 문법은 1980년대 Perl에서부터 사용되기 시작했습니다.
주로 검색 또는 치환 작업에 사용되며, 현재 대부분의 프로그래밍 언어에서 정규표현식을 지원하거나 라이브러리를 통해 사용할 수 있습니다.
예를 들어 다음 문장에서 특정 조건에 따른 검색을 진행하고자 할 때, 정규표현식을 사용할 수 있습니다.
The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it.
too를 찾고자 할 때 -> /too/ The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it.
대소문자 구분 없이 t를 모두 찾고자 할 때 -> /t/-gi The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it.
d로 시작하는 단어 및 문자를 모두 찾고자 할 때 -> /[d]\w+/-gi The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it.
ea가 들어가는 단어를 찾고자 할 때 -> /[a-z]*ea[a-z]*/-i The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it.
다음 사이트를 이용하면 의도한 대로 정규표현식이 작성되었는지 확인할 수 있어 매우 유용합니다.