From 310335223b0971a34faa8c69c73bd9e77c2adffe Mon Sep 17 00:00:00 2001 From: Jonathon Lee Date: Mon, 18 Apr 2016 23:38:53 +0100 Subject: [PATCH] fix(Regex): escape all characters to prevent exception --- components/select/common.ts | 3 +++ components/select/select-pipes.ts | 6 ++---- components/select/select.ts | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 components/select/common.ts diff --git a/components/select/common.ts b/components/select/common.ts new file mode 100644 index 00000000..a1e0053b --- /dev/null +++ b/components/select/common.ts @@ -0,0 +1,3 @@ +export function escapeRegexp(queryToEscape:string) { + return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1'); +} diff --git a/components/select/select-pipes.ts b/components/select/select-pipes.ts index 4ffb6eda..086a10dc 100644 --- a/components/select/select-pipes.ts +++ b/components/select/select-pipes.ts @@ -1,4 +1,5 @@ import {Pipe} from 'angular2/core'; +import {escapeRegexp} from './common'; @Pipe({ name: 'hightlight' @@ -18,7 +19,7 @@ export class HightlightPipe { // Replace tags with token let tmpValue = value.replace( tagRE, '$!$'); // Replace search words - value = tmpValue.replace(new RegExp(this.escapeRegexp(query), 'gi'), '$&'); + value = tmpValue.replace(new RegExp(escapeRegexp(query), 'gi'), '$&'); // Reinsert HTML for (let i = 0; value.indexOf('$!$') > -1; i++) { value = value.replace('$!$', tagList[i]); @@ -27,9 +28,6 @@ export class HightlightPipe { return value; } - private escapeRegexp(queryToEscape:string) { - return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1'); - } } export function stripTags(input:string) { diff --git a/components/select/select.ts b/components/select/select.ts index 055b9209..074deef0 100644 --- a/components/select/select.ts +++ b/components/select/select.ts @@ -17,6 +17,7 @@ import { stripTags } from './select-pipes'; import {IOptionsBehavior} from './select-interfaces'; +import {escapeRegexp} from './common'; let optionsTemplate = `