-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(autocomplete example): regex metacharacter escape #4921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(autocomplete example): regex metacharacter escape #4921
Conversation
autocomplete material example fails if regex metacharacter (ie *) is provided. Fixed by adding escapeSpecChars function
filterStates(val: string) { | ||
return val ? this.states.filter(s => new RegExp(`^${val}`, 'gi').test(s)) | ||
let escVal: string = this.escapeSpecChars(val); | ||
return val ? this.states.filter(s => new RegExp(`^${escVal}`, 'gi').test(s)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a cleaner solution would be to just not use a regex at all and instead just use indexOf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the hint. Decided to use includes
instead of indexOf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related #4440 (comment)
@@ -73,7 +73,7 @@ export class AutocompleteOverviewExample { | |||
} | |||
|
|||
filterStates(val: string) { | |||
return val ? this.states.filter(s => new RegExp(`^${val}`, 'gi').test(s)) | |||
return val ? this.states.filter(s => s.toLowerCase().includes(val.toLowerCase())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
includes
is not supported in IE11 (which we still support). We try to avoid requiring polyfills where possible
@willshowell good point; it should be specifically |
@jelbourn @willshowell how about adding a toggle "Search from beginning" in the example which would switch between |
It's just an example- I think the |
@jelbourn ok, fixed with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I know it's just an example, but with this change it behaves in a different way: Before: When "a" is typed it shows "Alabama", "Alaska", "Arizona", "Arkansas" Now: When "a" is typed it shows simply nothing, because Also, I think it should matches the whole string... example: If I type "k" it should match So, to make it work in the cases described above, I'd suggest to change it to:
|
@rafaelss95 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
autocomplete material example fails if regex metacharacters (ie *) are provided. Fixed by adding escapeSpecChars function