HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Common regular expressions

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptcommonexpressionsregular

Problem


  • Use the ^ and $ anchors to match the start and end of the string, respectively.
  • Add the string you want to match in-between the two anchors.
  • Use the ^ and $ anchors to match the start and end of the string, respectively.
  • Do not add any characters in-between to match an empty string.
  • Use the \s meta-sequence to match any whitespace character, including spaces, tabs, newlines, etc.

Solution

const regexp = /^abc$/;
// Where 'abc' is the exact string you want to match


  • Use the ^ and $ anchors to match the start and end of the string, respectively.
  • Do not add any characters in-between to match an empty string.
  • Use the \s meta-sequence to match any whitespace character, including spaces, tabs, newlines, etc.
  • Use the + quantifier to match one or more occurrences of the previous character.
  • Add the global flag (g) to match all occurrences of the pattern in the string.
  • Depending on the environment, line breaks can be represented in different ways.

Code Snippets

const regexp = /^abc$/;
// Where 'abc' is the exact string you want to match
const regexp = /^$/;
const regexp = /\s+/g;

Context

From 30-seconds-of-code: common-regexp-cheatsheet

Revisions (0)

No revisions yet.