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

These Are The Bash Shell Commands That Stand Between Me And Insanity

These Are The Bash Shell Commands That Stand Between Me And Insanity These Are The Bash Shell Commands That Stand Between Me And Insanity I will not profess to be a bash shell wizard… but I have managed to scour some pretty helpful little scripts from Stack Overflow and modify… These Are The Bash Shell Commands That Stand Between Me And Insanity I will not profess to be a bash shell wizard… but I have managed to scour some pretty helpful little scripts from Stack Overflow and modify them to suit my needs. All of these commands are for Ubuntu/WSL … some may work in other scenarios but I can’t guarantee it. ...
Deploy-React-App-To-Heroku-Using-Postgres Deploy React App To Heroku Using Postgres & Express Heroku is an web application that makes deploying applications easy for a beginner. Deploy React App To Heroku Using Postgres & Express Heroku is an web application that makes deploying applications easy for a beginner. Before you begin deploying, make sure to remove any console.log ’s or debugger ’s in any production code. You can search your entire project folder if you are using them anywhere. You will set up Heroku to run on a production, not development, version of your application. When a Node.js application like yours is pushed up to Heroku, it is identified as a Node.js application because of the package.json file. It runs npm install automatically. Then, if there is a heroku-postbui...

Data Structures Resources

Data Structures & Algorithms Resource List Part 1 Data Structures & Algorithms Resource List Part 1 Guess the author of the following quotes: Data Structures & Algorithms Resource List Part 1 Guess the author of the following quotes: Talk is cheap. Show me the code. Software is like sex: it’s better when it’s free. Microsoft isn’t evil, they just make really crappy operating systems. Update: Here’s some more: ...