u modifier를 이용하여 정규표현식에 유니코드를 사용할 수 있음.
유니코드에 정의된 언어셋별로도 활용할 수 있어 유용할 듯.
const s = 'a가韓あアb나灭いイ';
[
'Hangul', 'Hiragana', 'Katakana', 'Han'
].forEach((script)=>{
const pattern = new RegExp(`\\\p{Script=${script}}`, 'gu');
const matches = s.match(pattern);
console.log(`[${script}]`);
console.log(matches);
});
// 출력
/*
[Hangul]
['가', '나']
[Hiragana]
['あ', 'い']
[Katakana]
['ア', 'イ']
[Han]
['韓', '灭']
*/
이모지도 됨.
const s = 'a❤가✔あ😀ア🍖0';
['Emoji', 'Emoji_Presentation'].forEach((binaryUnicodeProperty)=>{
const pattern = new RegExp(`\\\p{${binaryUnicodeProperty}}`, 'gu');
const matches = s.match(pattern);
console.log(`[${binaryUnicodeProperty}]`);
console.log(matches);
});
// 출력
/*
[Emoji]
['❤', '✔', '😀', '🍖', '0']
[Emoji_Presentation]
['😀', '🍖']
*/
[참고]
https://www.regular-expressions.info/unicode.html
'JavaScript' 카테고리의 다른 글
[JavaScript] 유니코드 정규표현식 (regular expressions - unicode) (0) | 2022.05.11 |
---|---|
[JavaScript] 정기예금 월복리 계산식 (네이버 이자계산기 기준) (0) | 2021.10.17 |
[JavaScript] YYYY-MM-dd hh:mm:ss Date format (0) | 2021.03.09 |
[JavaScript] 정규식 캡쳐 그룹 이름 지정 (regex named capturing group, non-capturing group) (0) | 2020.06.30 |
[JavaScript] input type="week" value를 javascript로 세팅하기 (ISO 8601 week number) (0) | 2020.06.11 |
[JavaScript] Option chaining(?.), Nullish coalescing operator(??) (0) | 2020.05.20 |
댓글을 달아 주세요