type-ahead.js icon indicating copy to clipboard operation
type-ahead.js copied to clipboard

escape regex special characters

Open kruczy opened this issue 10 years ago • 5 comments

Escape regexp special characters to prevent "invalid regexp" errors and unexpected behaviour while querying for candidates

kruczy avatar Feb 10 '16 16:02 kruczy

Initial look LGTM. I'll pull your changes and see how they hold up under some tests (which reminds me, we need to get tests in here...), and will check back in tonight most likely.

therebelrobot avatar Feb 10 '16 16:02 therebelrobot

Thanks for quick reply, you can try something like "c++" "test(" or any combination of regexp special chars that will not create a valid one

could also move the regexp to a static so browser wont have to do so much work

kruczy avatar Feb 10 '16 16:02 kruczy

Will definitely test those, I've also got a few more I can try. Will you add as many test cases as you can think of over on #21? I want to make sure I'm hitting as many as possible when building out tests.

could also move the regexp to a static so browser wont have to do so much work

Could you elaborate more on this? This lib needs to be able to be run entirely browser-side, so I'm not sure how you'd unload some of that off. (maybe I'm just misunderstanding?)

therebelrobot avatar Feb 10 '16 16:02 therebelrobot

what I'm saying is quite simple, just TypAhead.FILTER_SANITIZE_REGEXP = /..../ and use that later

I guess browsers can optimize for inline regex use tho, so should not make much difference

testing on IE 11, Edge, Chrome and FF seems only IE would benefit from this in a meaningful way, probably should just leave it as is for readibility

kruczy avatar Feb 10 '16 16:02 kruczy

If you'd like to move it to a separate declaration, feel free. So long as it works, I don't have a preference.

therebelrobot avatar Feb 10 '16 17:02 therebelrobot