Skip to content

regexp ​

regexp parses a string that matches a provided regular expression. Returns the matched string, or fails with an expected message.

Implementation notes ​

WARNING

Matching is performed in sticky mode, so g and y flags are handled automatically.

The regular expression must obey one simple rule: it doesn't use ^ and $ to match at the beginning or at the end of the text. Flags like u and i are allowed and can be added if needed.

The match must begin at the current position. A Unicode regular expression positioned inside a surrogate pair fails instead of consuming the whole pair.

Usage ​

ts
const Parser = regexp(/\p{Emoji_Presentation}/gu, 'emoji')

Success

ts
run(Parser).with('👌')

{
  isOk: true,
  start: 0,
  end: 2,
  pos: 2,
  value: '👌'
}

Failure

ts
run(Parser).with('大')

{
  isOk: false,
  start: 0,
  end: 0,
  pos: 0,
  expected: 'emoji'
}