사용자 도구

사이트 도구


wiki:javascript:javascript_note:javascript_regexp_group_abc

JavaScript RegExp Group [abc]

  • description : JavaScript RegExp Group [abc]
  • author : 오션
  • email : shlim@repia.com
  • lastupdate : 2023-01-06 Fri


문자열 안에서 문자 “h”에 대한 전역 검색

let text = "Is this all there is?";
let pattern = /[h]/g;
let result = text.match(pattern);
 
console.log(result);                // (2) ['h', 'h']
console.log(typeof result);         // object
console.log(Array.isArray(result)); // true


Definitiion and Usage

대괄호[abc]는 대괄호 안의 문자와 일치하는 것을 찾으라는 것을 명시합니다.
대괄호는 단일 문자들, 문자 그룹, 또는 문자 범위를 정의할 수 있습니다.

[abc] a,b,c 세 개의 문자와 일치하는 각각의 문자들
[A-Z] 대문자 A부터 대문자 Z까지 모든 대문자
[a-z] 소문자 a부터 소문자 z까지의 모든 소문자
[A-z] 대문자 A부터 소문자 z까지의 모든 문자들


Syntax

new RegExp("[abc]")
 
// or simply:
 
/[abc]/


Syntax with modifiers

new RegExp("[abc]", "g")
 
// or simply:
 
/[abc]/g

Tip

브래킷 안에 없는 문자를 찾기 위해서는 [^abc]를 사욥합니다.

문자열에서 문자 “i”, “s”에 대한 전역 검색하기

let text2 = "Do you know if this is all there IS?";
let pattern2 = /[is]/g;
let result2 = text2.match(pattern2);
console.log(result2);   //  [ 'i', 'i', 's', 'i', 's' ]
console.log(Array.isArray(result2));    // true
console.log(result2.length);    // 5
 
 
let text3 = "Do you know if this is all there IS?";
let pattern3 = /[is]/gi;
let result3 = text3.match(pattern3);
console.log(result3);   //['i', 'i', 's','i', 's', 'I','S']
console.log(Array.isArray(result3));    // true
console.log(result3.length);    // 7
/var/services/web/dokuwiki/data/pages/wiki/javascript/javascript_note/javascript_regexp_group_abc.txt · 마지막으로 수정됨: 2023/01/13 18:44 (바깥 편집)