Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
JSにおける正規表現
Search
ls_pr
March 11, 2021
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
JSにおける正規表現
ls_pr
March 11, 2021
More Decks by ls_pr
See All by ls_pr
戦場で生き抜く 炎上案件の耐え方
ls_pr
0
260
自作PCのすすめ
ls_pr
0
180
ツボを押さえたWFの作り方
ls_pr
0
180
Linuxのちょこっと調べ
ls_pr
0
170
色々な色の話~今日から使える簡単配色テクニック3つ~
ls_pr
0
300
ゼロから始めるフロントエンドモジュール開発についてあれこれ
ls_pr
0
170
ejsのすすめ
ls_pr
0
220
Vue.jsの機能を使って色々作ってみた
ls_pr
0
500
NuxtではじめるVue.js
ls_pr
0
330
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
A designer walks into a library…
pauljervisheath
211
25k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
570
Building the Perfect Custom Keyboard
takai
2
950
Rails Girls Zürich Keynote
gr2m
96
14k
RailsConf 2023
tenderlove
30
1.6k
WCS-LA-2024
lcolladotor
0
980
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
510
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.7k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Transcript
JSにおける正規表現 中村 祐太
目次/概要 1. 正規表現とは 2. JSの主な正規表現パターン 3. 正規表現オブジェクトの生成 4. 動作オプションの設定 個人的に疑問の多かった正規表現を、ざっくりと勉強した結果の発表。
正規表現の基礎の基礎を説明。 具体的な応用・活用等には踏み込まない。
1. 正規表現(Regular Expression)とは • 文字列をパターン化して表現する仕組み • 条件に合う文字列を検索し、処理を行える ・文字列が条件と適合するか「テスト」する ・条件に合う文字列を「取得」する ・〃指定した区切り文字で「分割」する
・〃新しい文字列に「置換」する → test() → match(),exec() → split() → replace() 目的の処理 併用するメソッド
1. 正規表現(Regular Expression)とは 「文字列をパターン化」? 郵便番号 0~9の数字3桁 + “-” + 0~9の数字4桁
携帯電話番号 0 + 7~9のどれか1つ + 0 + “-” + 0~9の数字4桁 + “-” + 0~9の数字4桁 000-0000 080-1234-5678
2. JSの主な正規表現パターン 基本形 量指定 位置指定 文字セット ABC [ABC] [^ABC] [A-Z]
A|B|C X* X? X+ X{n} X{n,} X{m, n} ^ $ . \w \W \d \D \n \r \t \s \S \~
2. JSの主な正規表現パターン 郵便番号 携帯電話番号 0~9の数字3桁 + “-” + 0~9の数字4桁 0
+ 7~9のどれか1つ + 0 + “-” + 0~9の数字4桁 + “-” + 0~9の数字4桁 ^[0-9]{3}-[0-9]{4}$ または ^\d{3}-\d{4}$ ^0[789]0-[0-9]{4}-[0-9]{4}$ または ^0[789]0-\d{4}-\d{4}$ ^ :文字列の先頭を指定 $ :文字列の末尾を指定 [0-9] :0~9の数字 {n} :直前に指定した文字とn回一致 \d :数字を示す([0-9と同意])
3. 正規表現オブジェクトの生成 方法① コンストラクターを使用 方法② 正規表現リテラルを使用 正規表現は文字列として指定するため、バックスラッシュ(\)の挙動に注意 (※JSの文字列において、 “\”は意味を持った予約文字) 正規表現パターンそのものにスラッシュ(/)を使用する場合は、エスケープ処理を行う
(※正規表現において、 “/”は意味を持った予約文字)
3. 正規表現オブジェクトの生成 余談 「RegExpオブジェクト」と簡単に書きましたが、 肝心の”RegExp”の読み方はハッキリしていません。 最後の”p”の発音が特に困るようで、 最早”p”は無視して「れげっくす」「れじぇっくす」と読んだり、 割り切ってフルネームで「レギュラーエクスプレッション」と読んだり、 全てを諦めて「正規表現」と読んでしまう事もあるようです。 今回の件に限らず、読み方が今一つハッキリしないプログラミング用語を、
他の方はどう読んでいるのか気になったりもします。
4. オプション(フラグ)の設定 「g」「i」「m」「u」等のオプションを付け加えることで、 パターンに応じた検索の挙動を制御出来る。 フラグ フラグ名 効果 g Global 初めの1件のみでなく、文字列全体を検索対象にする
i Ignorecase 大文字/小文字の違いを無視する m Multiline 「^」「$」が「行頭」「行末」を示すようになる u Unicode サロゲートペア文字(※)が認識可能になる ※サロゲートペア文字: 本来は1文字を2バイトで表現するUTF-8が、扱える文字数の拡張のため4バイトで表現している文字。 主要なオプションは以下↓