diff --git a/packages/forms/src/validators.ts b/packages/forms/src/validators.ts index 8f8f6cfa70..63a962b051 100644 --- a/packages/forms/src/validators.ts +++ b/packages/forms/src/validators.ts @@ -163,7 +163,14 @@ export class Validators { let regex: RegExp; let regexStr: string; if (typeof pattern === 'string') { - regexStr = `^${pattern}$`; + regexStr = ''; + + if (pattern.charAt(0) !== '^') regexStr += '^'; + + regexStr += pattern; + + if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$'; + regex = new RegExp(regexStr); } else { regexStr = pattern.toString(); diff --git a/packages/forms/test/validators_spec.ts b/packages/forms/test/validators_spec.ts index 1cd9acdbb1..a30c4430be 100644 --- a/packages/forms/test/validators_spec.ts +++ b/packages/forms/test/validators_spec.ts @@ -270,6 +270,12 @@ import {map} from 'rxjs/operator/map'; it('should not error on "undefined" pattern', () => expect(Validators.pattern(undefined !)(new FormControl('aaAA'))).toBeNull()); + + it('should work with string containing line boundary', + () => expect(Validators.pattern('^[aA]*$')(new FormControl('aaAA'))).toBeNull()); + + it('should work with string not containing line boundary', + () => expect(Validators.pattern('[aA]*')(new FormControl('aaAA'))).toBeNull()); }); describe('compose', () => {