正規表現



正規表現

  • 正規表現(regexpまたはregと略称する)は、モードおよびオプションのフラグ(flag)から構成される.
  • 正規オブジェクトを作成する場合、次の2つの構文があります.
  • // 긴 문법 : 생성자 함수 방식
    const regexp1 = new RegExp("pattern", "flags");
    
    // 짧은 문법 : 리터럴(Literal) 방식
    const regexp2 = /pattern/; // 플래그가 없음
    const regexp3 = /pattern/gmi; // 플래그 g, m, i가 있음(각 플래그에 대해선 곧 다룰 예정)
  • スラッシュ"/"は、JavaScriptが正規表現を生成していることを示します.文字列で引用符を使用するのと同じ役割を果たします.
  • いずれの構文を使用しても、上記の例のregexpは、クラスRegExpを内蔵するオブジェクトになります.
  • の2つの構文の重要な違いは、フレーズ法/.../を使用すると、文字列テンプレートで${...}を使用するように、中間に式を挿入することができないことです.つまり、完全に静的な表現です.
  • フレーズ法は、コードを記述する際にモードを理解する際に非常に有用である.多くはこのような状況に属している.逆に、長い構文は主に状況に応じて動的に生成される文字列に使用され、正規表現を作成する必要があります.関連例を見てみましょう.
  • let tag = prompt("어떤 태그를 찾고 싶나요?", "h2");
    
    // 프롬프트에서 "h2"라고 대답한 경우, /<h2>/와 동일한 역할을 한다.
    let regexp1 = new RegExp(`<${tag}>`);
    
    // 정규표현식에 변수를 넣기 위해서도 사용한다.
    let regexp2 = new RegExp(`{${'변수명'},${'변수명'}}`);

    ひょうしき

  • は正規表現のオプションとしてマークされ、検索する文字モードに追加のオプションを追加して、必要な文字検索結果を返します.
  • フラグは、複数同時に使用することができる.
  • const str = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`;
    
    str.match(/ipsum/);
    // null
    
    str.match(/ipsum/i);
    // ["Ipsum", index: 6, input: "Lorem Ipsum is simply dummy text of the printing a…ldus PageMaker including versions of Lorem Ipsum.", groups: undefined]
    
    str.match(/ipsum/ig);
    // (4) ["Ipsum", "Ipsum", "Ipsum", "Ipsum"]
    
    const strMultyLine = `Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`;
    
    strMultyLine.match(/$/g);
    // [""]
    
    strMultyLine.match(/$/gm);
    // ["", "", "", ""]

    を選択します。

  • 式の各種特殊記号(パターン)はその記号の意味(機能)と一致せず認識できず,単独で暗記しなければその意味を把握できない.



  • 方法

  • 正規表現には、さまざまな方法があります.
  • const str = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`;
    
    // 1. 정규식.메소드(문자열)
    console.log(/ipsum/.exec(str));
    // null
    
    console.log(/ipsum/i.exec(str));
    // ["Ipsum", index: 6, input: "Lorem Ipsum is simply dummy text of the printing a…ldus PageMaker including versions of Lorem Ipsum.", groups: undefined]
    
    console.log(/ipsum/i.test(str));
    // true
    
    // 2. 문자열.메소드(정규식)
    console.log(str.match(/ipsum/));
    // null
    
    console.log(str.match(/ipsum/i));
    // ["Ipsum", index: 6, input: "Lorem Ipsum is simply dummy text of the printing a…ldus PageMaker including versions of Lorem Ipsum.", groups: undefined]
    
    console.log(str.search(/ipsum/i));
    // 6
    
    console.log(str.replace(/ipsum/i, 'Hoon'));
    // 기존 문자열은 유지되며 새로운 문자열이 반환된다.
    // Lorem Hoon is simply dummy text of the printing and typesetting industry. Lorem Hoon has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Hoon passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Hoon.
    
    console.log(str.split(/ipsum/i));
    // ['Lorem ', ' is simply dummy text of the printing and typesetting industry. Lorem ', " has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem ", ' passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem ', '.'];

    参考資料

  • https://droptable.tistory.com/65
  • https://heropy.blog/2018/10/28/regexp/
  • https://beomy.tistory.com/21
  • https://poiemaweb.com/js-regexp
  • https://curryyou.tistory.com/234