Skip to content
Hyunseung Ryu edited this page Aug 11, 2023 · 5 revisions

Pattern is a JavaScript library that provides a builder to construct regular expressions.

Here's how to implement /-?(\d+)/ as a Pattern.

// same as /-?(\d+)/
const isInteger = Pattern(_ => _
    .maybe('-')  
    .capture(g1 => g1
        .digit().oneOrMore()
    )
);

See the Rules page for more syntax.

The Pattern function returns a PatternEx type.
Since PatternEx inherits from RegExp, all prototype functions of RegExp can be used as is.

Few exceptions
  • String.replace(RegExp, String) -> PatternEx.replace(String, String)
  • String.match(RegExp) -> PatternEx.match(String)
console.log(isInteger.test('-234'));    // true
console.log(isInteger.match('where is integer 1234 where'));
console.log(isInteger.replace('where is integer 1234 where', 'integer'));    // where is integer integer where
Clone this wiki locally