1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ググって出てきた正規表現がさっぱり理解できない時に使えるハック

Posted at

ググって出てきた正規表現がさっぱり理解できない時に使えるハックです。
正規表現のみたこともないやつ、たくさんあると思います。

例えばとある接頭語から”始まらない”バリデーションを書きたいとする。

以下のようにググりました。
check not start with prefix regexp

このstack overflowが最初に出てきました。
https://stackoverflow.com/questions/2116328/regexp-matching-string-not-starting-with-my

どうやら
^(?!my)\w+$
この表現が使えそうです。

?!の表現なんてみたことないです。

ここで活躍するのがこのサイト => https://regex101.com/
正規表現書いたことある人は絶対ブックマークに入れたほうがいいです。

どうやら意図通りに動きそうです。
image.png

でも裏を取らないと、本当に意図としてあっているのか不安です。
実は右のほうに、その表現がどういう名前で呼ばれているか書かれています。

貼り付けた画像_2021_01_27_18_33.png

なので、Negative Lookaheadで改めてググると、以下の解説サイトが出てきました。
https://ja.javascript.info/regexp-lookahead-lookbehind

めでたしめでたし。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?