Jon Jon - 1 year ago 61
Javascript Question

Regex JS - Create Capture Pattern Without Specified Word

I am trying to capture with Javascript regex any string between my domain and

(if present), but am having trouble doing so. Any advice?

www\.mysite\.com\/(.*)(\.html) // Does not capture ''
www\.mysite\.com\/(.*)(\.html)? // Captures the '.html'

Test Data: (capture group should be 'aadvark') (capture group should be 'bird') (capture group should be 'cat')

Sam Sam
Answer Source

A lot of issues like this can be fixed by being more specific with your dot-match-all. If you change your .* to [^.]* (0+ non-. characters), you'll get your expected results.


This is because when you make (\.html) optional, the .* greedily continues to the end. This could also be fixed by using ? to make your repetition "lazy" (stops as soon as the next part of the expression matches); however, then you'd need to anchor the end of the expression with a $.


I'd recommend this first. But, the second is more encompassing by matching things like in

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download