I've created an extensive regex pattern to identify URLs such as:
/^\/([^\/.?]+)(?:\/([^\/.?]+)(?:\/([^\/.?]+)(?:\.([^\/.?]+))?)?)?$/
It successfully matches:
/foo/bar/1.html
and returns ['foo', 'bar', '1', 'html']
As I work in Javascript, I am looking to extract the matched parts as the user types the URL (similar to a typeahead feature). For instance, if the user types:
/foo
The code should recognize that /foo
has been matched, even though the entire regex pattern has not been satisfied. In Ruby, it's possible to retrieve an array containing only the partially matched elements like: ['foo', nil, nil, nil]
. Is it also feasible, or straightforward, to achieve this in Javascript?