There are often cases where a regular expression for custom validation, parsing, masking or replacing is required by a controller. Currently this would need to be handled bespoke within the controller logic.
This issue proposes a new RegExp value type that abstracts away the common logic required for controllers that want to handle a 'pattern' (or even flags) passed in via data attributes to a controller.
Some assumptions could be made about the appropriate flags to use for the common case (e.g. v, same as input pattern) and then any modifications to these can happen within the Stimulus controller.
When decoding, an empty string will be used as the default, to avoid unintended undefined leaking into RegExp.
false may not work as expected, unless we change the decoding behaviour, RegExp(false) gets coerced into a string of 'false'. e.g. we could decode as RegExp((value == "0" || value == "false") ? '' : value)
Usage
<div data-my-controller-pattern-value=""></div>
<!-- Equivalent to /(?:)/v -->
<div data-my-controller-pattern-value="[0-9]{3}"></div>
<!-- Equivalent to /[0-9]{3}/v -->
<div data-my-controller-pattern-value="[0-9]{3}/[a-z]*"></div>
<!-- Equivalent to /[0-9]{3}\/[a-z]*/v (note: no escaping needed for inner slash) -->
There are often cases where a regular expression for custom validation, parsing, masking or replacing is required by a controller. Currently this would need to be handled bespoke within the controller logic.
This issue proposes a new
RegExp
value type that abstracts away the common logic required for controllers that want to handle a 'pattern' (or even flags) passed in via data attributes to a controller.Proposed implementation 1 (with flags)
A new type would be added to the documentation https://stimulus.hotwired.dev/reference/values#types
RegExp
value.toString()
RegExp(...Object.values(/^\/(?<a>.+)\/(?<b>[a-z]*)$/.exec(value \|\| '/(?:)/').groups))
/some|pattern/gy/
(with leading & trailing slashes).RegExp('')
as a regex that matches only an empty string/(?:)/
.toString
method will be called on the RegExp instance, resolving to the same pattern & flags with slashes value.Usage
Proposed implementation 2 (without flags)
This alternative approach is a simpler (no flags) option, where the RegExp pattern is provided as is and there is no need to escape slashes.
This could mean we can align with the behaviour in
input
pattern
attributes - https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/patternA new type would be added to the documentation https://stimulus.hotwired.dev/reference/values#types
RegExp
value.source
RegExp(value \|\| '', 'v')
v
, same as input pattern) and then any modifications to these can happen within the Stimulus controller.source
to provide the pattern string will escape slashes when output https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sourceundefined
leaking intoRegExp
.false
may not work as expected, unless we change the decoding behaviour,RegExp(false)
gets coerced into a string of 'false'. e.g. we could decode asRegExp((value == "0" || value == "false") ? '' : value)
Usage
Prior art
pattern
attribute on the input element. https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern