JavaScriptで正規表現
  JavaScript(ECMAScript)で正規表現を使う方法のまとめ 
  
・ECMAScriptで正規表現を使うには「RegExp」クラス、「String」クラスの一部のメソッドを使う
  
・RegExpクラスを使うには3つの方法がある
  
  
・testメソッド (マッチングするか調べる)
  
    
  
・execメソッド (マッチングした文字列を取り出す)
  
    
  
・Stringクラス+matchメソッド (マッチングした文字列を配列で取得)
  
    
  
・Stringクラス+matchAllメソッド (マッチングした文字列を反復子で取得)
  
・Stringクラス+searchメソッド (マッチングした文字列の位置)
  
・Stringクラス+replaceメソッド (マッチングした文字列を別の文字列に置き換え)
  
・Stringクラス+replaceAllメソッド (マッチングした文字列を全て置換)
  
・ECMAScriptで正規表現を使うには「RegExp」クラス、「String」クラスの一部のメソッドを使う
・RegExpクラスを使うには3つの方法がある
    1. コンストラクタにマッチングパターン、フラグを指定 
    
2. リテラル記法 (一般的な使い方)
    
3. コンストラクタを介したリテラル記法
    
      
  
  
      ※作成されるたびにコンパイル (ループ中でパターンが変換する場合に使う) 
    
    
      const regex = new RegExp('パターン'); // RegExp('oo'); 
const regex = new RegExp('パターン', 'フラグ'); // RegExp('oo', 'g');
    
    const regex = new RegExp('パターン', 'フラグ'); // RegExp('oo', 'g');
2. リテラル記法 (一般的な使い方)
      ※評価時にコンパイル (ループ中で毎回同じパターンを使う場合は効率が良い) 
    
    
      const regex = /パターン/; // regex = /oo/'; 
const regex = /パターン/フラグ; // regex = /oo/g;
    
    const regex = /パターン/フラグ; // regex = /oo/g;
3. コンストラクタを介したリテラル記法
        const regex = new RegExp(/パターン/); // RegExp(/oo/); 
const regex = new RegExp(/パターン/, 'フラグ'); // RegExp(/oo/, 'g');
      
    const regex = new RegExp(/パターン/, 'フラグ'); // RegExp(/oo/, 'g');
・testメソッド (マッチングするか調べる)
      const regex = /[\d]{4}/; // 4桁の数字 
const str = '225-0002';
console.log(regex.test(str)); // true
    
  const str = '225-0002';
console.log(regex.test(str)); // true
・execメソッド (マッチングした文字列を取り出す)
      const regex = /[\d]{4}/; 
const str = '225-0002';
const array = regex.exec(str);
if (array !== null) {
console.log('${array[0]}'); // 0002
}
    
  const str = '225-0002';
const array = regex.exec(str);
if (array !== null) {
console.log('${array[0]}'); // 0002
}
・Stringクラス+matchメソッド (マッチングした文字列を配列で取得)
      const regex = /[\d]{3,4}/g; 
const str = '225-0002';
const array = str.match(regex);
if (array !== null) {
console.log(array); // ['225', '0002']
}
    
  const str = '225-0002';
const array = str.match(regex);
if (array !== null) {
console.log(array); // ['225', '0002']
}
・Stringクラス+matchAllメソッド (マッチングした文字列を反復子で取得)
    ※gフラグを指定すること 
    
  
      const regex = /[\d]{3,4}/g; 
const str = '225-0002';
const array = str.matchAll(regex);
for (const match of array) {
console.log(match[0]); // 225 と 0002
}
    
  const str = '225-0002';
const array = str.matchAll(regex);
for (const match of array) {
console.log(match[0]); // 225 と 0002
}
・Stringクラス+searchメソッド (マッチングした文字列の位置)
    ※先頭は0、ヒットしなければ-1 
    
  
      const str = '225-0002'; 
const regex1 = /[\d]{4}/;
console.log(str.search(regex1)); // 4
const regex2 = /[\d]{5}/;
console.log(str.search(regex2)); // -1
    
  const regex1 = /[\d]{4}/;
console.log(str.search(regex1)); // 4
const regex2 = /[\d]{5}/;
console.log(str.search(regex2)); // -1
・Stringクラス+replaceメソッド (マッチングした文字列を別の文字列に置き換え)
    ※gフラグを指定するとマッチングした全ての文字列を置換 
    
  
      const regex = /[\d]{4}/; 
const str = '225-0002-0003';
console.log(str.replace(regex, '9999')); // 225-9999-0003
const regex2 = /[\d]{4}/g;
console.log(str.replace(regex2, '9999')); // 225-9999-9999
    
  const str = '225-0002-0003';
console.log(str.replace(regex, '9999')); // 225-9999-0003
const regex2 = /[\d]{4}/g;
console.log(str.replace(regex2, '9999')); // 225-9999-9999
・Stringクラス+replaceAllメソッド (マッチングした文字列を全て置換)
    ※gフラグを指定すること 
    
      const regex = /o{2}/g; 
const str = 'Boo Foo Woo';
console.log(str.replaceAll(regex, 'ee')); // Bee Fee Wee
    
  const str = 'Boo Foo Woo';
console.log(str.replaceAll(regex, 'ee')); // Bee Fee Wee
JavaScript, 正規表現
 
0 件のコメント:
コメントを投稿