Skip to main content

Regular Expressions in JavaScript

Regular Expressions In JavaScript

Regular Expressions In JavaScript

Basic:

Regular Expressions In JavaScript



Basic:

Anchors


  • ^ Start of string or line
  • \A Start of string
  • $ End of string or line
  • \Z End of string
  • \b Word boundary
  • \B Not word boundary
  • \< Start of word
  • \> End of word

Character Classes


  • \c Control character
  • \s Whitespace [ \t\r\n\v\f]
  • \S Not Whitespace [^ \t\r\n\v\f]
  • \d Digit [0–9]
  • \D Not digit [⁰-9]
  • \w Word [A-Za-z0–9_]
  • \W Not Word [^A-Za-z0–9_]
  • \x Hexadecimal digit [A-Fa-f0–9]
  • \O Octal Digit [0–7]

POSIX Classes


  • [:upper:] Uppercase letters [A-Z]
  • [:lower:] Lowercase letters [a-z]
  • [:alpha:] All letters [A-Za-z]
  • [:alnum:] Digits and letters [A-Za-z0–9]
  • [:digit:] Digits [0–9]
  • [:xdigit:] Hexadecimal digits [0–9a-f]
  • [:punct:] Punctuation
  • [:blank:] Space and tab [ \t]
  • [:space:] Blank characters [ \t\r\n\v\f]
  • [:cntrl:] Control characters [\x00-\x1F\x7F]
  • [:graph:] Printed characters [\x21-\x7E]
  • [:print:] Printed characters and spaces [\x20-\x7E]
  • [:word:] Digits, letters and underscore [A-Za-z0–9_]

Pattern Modifiers


  • //g Global Match (all occurrences)
  • //i Case-insensitive
  • //m Multiple line
  • //s Treat string as single line
  • //x Allow comments and whitespace
  • //e Evaluate replacement
  • //U Ungreedy pattern

Escape Sequences


  • \ Escape following character
  • \Q Begin literal sequence
  • \E End literal sequence

Quantifiers


  • * 0 or more
  • + 1 or more
  • ? 0 or 1 (optional)
  • {3} Exactly 3
  • {3,} 3 or more
  • {2,5} 2, 3, 4 or 5

Groups and Ranges


  • . Any character except newline (\n)
  • (a|b) a or b
  • (…) Group
  • (?:…) Passive (non-capturing) group
  • [abc] Single character (a or b or c)
  • [^abc] Single character (not a or b or c)
  • [a-q] Single character range (a or b … or q)
  • [A-Z] Single character range (A or B … or Z)
  • [0–9] Single digit from 0 to 9

Assertions

  • ?= Lookahead assertion
  • ?! Negative lookahead
  • ?<= Lookbehind assertion
  • ?!= / ?<! Negative lookbehind
  • ?> Once-only Subexpression
  • ?() Condition [if then]
  • ?()| Condition [if then else]
  • ?# Comment

Special Characters

  • \n New line
  • \r Carriage return
  • \t Tab
  • \v Vertical tab
  • \f Form feed
  • \ooo Octal character ooo
  • \xhh Hex character hh

String Replacement

  • $n n-th non-passive group
  • \(2 “xyz” in /^(abc(xyz))\)/
  • \(1 “xyz” in /^(?:abc)(xyz)\)/
  • $` Before matched string
  • $’ After matched string
  • $+ Last matched string
  • $& Entire matched string

Comments

Popular posts from this blog

links

links Absolutely Everything You Could Need To Know About How JavaScript TOC & Condensed Links **** **** **** **** **** 1 2 3 4 5 leonardomso/33-js-concepts *This repository was created with the intention of helping developers master their concepts in JavaScript. It is not a…*github.com Call stack - MDN Web Docs Glossary: Definitions of Web-related terms MDN *A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its…*developer.mozilla.org Understanding Javascript Function Executions — Call Stack, Event Loop , Tasks & more *Web developers or Front end engineers, as that’s what we like to be called, nowadays do everything right from acting as…*medium.com Understanding the JavaScript call stack *The JavaScript engine (which is found in a hosting environment like the browser), is a single-threaded interpreter…*medium.freecodecamp.org Javascript: What Is The Execution Context? ...

Breaking Down Scope, Context, And Closure In JavaScript In Simple Terms.

Breaking Down Scope, Context, And Closure In JavaScript In Simple Terms. Breaking Down Scope, Context, And Closure In JavaScript In Simple Terms. “JavaScript’s global scope is like a public toilet. You can’t avoid going in there, but try to limit your contact with surfaces when you… Breaking Down Scope, Context, And Closure In JavaScript In Simple Terms. Photo by Florian Olivo on  Unsplash “ J avaScript’s global scope is like a public toilet. You can’t avoid going in there, but try to limit your contact with surfaces when you do.” ― Dmitry Baranowski Here’s another (much) more simple article I wrote on the subject: Closures In Javascript Answer A closure is a function defined...

webdev resource list part 2

Web Developer Resource List Part 2 &#9660; Web Developer Resource List Part 2 Because I compile these things compulsively anyway… Web Developer Resource List Part 2 Because I compile these things compulsively anyway… Ansible Awesome Lists CI/CD ...