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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
ls_pr
March 11, 2021
180
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
160
色々な色の話~今日から使える簡単配色テクニック3つ~
ls_pr
0
290
ゼロから始めるフロントエンドモジュール開発についてあれこれ
ls_pr
0
170
ejsのすすめ
ls_pr
0
210
Vue.jsの機能を使って色々作ってみた
ls_pr
0
490
NuxtではじめるVue.js
ls_pr
0
320
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
42
3.2k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Mobile First: as difficult as doing things right
swwweet
225
10k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
540
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
420
Building a Scalable Design System with Sketch
lauravandoore
464
34k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Believing is Seeing
oripsolob
1
210
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
350
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バイトで表現している文字。 主要なオプションは以下↓