正規表現
15233 ワード
正規表現
regexp
またはreg
と略称する)は、モードおよびオプションのフラグ(flag)から構成される.// 긴 문법 : 생성자 함수 방식
const regexp1 = new RegExp("pattern", "flags");
// 짧은 문법 : 리터럴(Literal) 방식
const regexp2 = /pattern/; // 플래그가 없음
const regexp3 = /pattern/gmi; // 플래그 g, m, i가 있음(각 플래그에 대해선 곧 다룰 예정)
"/"
は、JavaScriptが正規表現を生成していることを示します.文字列で引用符を使用するのと同じ役割を果たします.regexp
は、クラスRegExp
を内蔵するオブジェクトになります. /.../
を使用すると、文字列テンプレートで${...}
を使用するように、中間に式を挿入することができないことです.つまり、完全に静的な表現です.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 ', '.'];
参考資料
Reference
この問題について(正規表現), 我々は、より多くの情報をここで見つけました https://velog.io/@codenmh0822/정규-표현식Regular-Expressionテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol