diff --git a/lib/formatter.js b/lib/formatter.js
index 83c7f6d..a9e38a5 100644
--- a/lib/formatter.js
+++ b/lib/formatter.js
@@ -511,7 +511,7 @@ pattern.parse = function (pattern) {
 
   // Process match or add chars
   for (i; i < pLength; i++) {
-    if (i == matches[mCount].index) {
+    if (mCount < matches.length && i == matches[mCount].index) {
       processMatch(matches[mCount][1]);
     } else {
       info.chars[i - (mCount * DELIM_SIZE)] = pattern.charAt(i);
@@ -522,6 +522,7 @@ pattern.parse = function (pattern) {
   info.mLength = i - (mCount * DELIM_SIZE);
   return info;
 };
+
 //
 // Parse a matcher string into a RegExp. Accepts valid regular
 // expressions and the catchall '*'.
diff --git a/lib/formatter.min.js b/lib/formatter.min.js
index 07648b5..d2f48ad 100644
--- a/lib/formatter.min.js
+++ b/lib/formatter.min.js
@@ -6,4 +6,4 @@
  * thanks to digitalBush/jquery.maskedinput for some of the trickier
  * keycode handling
  */
-!function(a,b,c){"undefined"!=typeof module&&module.exports?module.exports=c():"function"==typeof define&&define.amd?define(c):b[a]=c()}("Formatter",this,function(){function a(a,d){var e=this;if(e.el=a,!e.el)throw new TypeError("Must provide an existing element");if(e.opts=k.extend({},c,d),"undefined"!=typeof e.opts.pattern&&(e.opts.patterns=e._specFromSinglePattern(e.opts.pattern),delete e.opts.pattern),"undefined"==typeof e.opts.patterns)throw new TypeError("Must provide a pattern or array of patterns");e.patternMatcher=b(e.opts.patterns),e._updatePattern(),e.hldrs={},e.focus=0,k.addListener(e.el,"keydown",function(a){e._keyDown(a)}),k.addListener(e.el,"keypress",function(a){e._keyPress(a)}),k.addListener(e.el,"paste",function(a){e._paste(a)}),e.opts.persistent&&(e._processKey("",!1),e.el.blur(),k.addListener(e.el,"focus",function(a){e._focus(a)}),k.addListener(e.el,"click",function(a){e._focus(a)}),k.addListener(e.el,"touchstart",function(a){e._focus(a)}))}function b(a){var b=[],c=[];k.forEach(a,function(a){k.forEach(a,function(a,d){var f=e.parse(a),g=i(d);return b.push(g),c.push(f),!1})});var d=function(a){var d;return k.forEach(b,function(b,c){return b.test(a)?(d=c,!1):void 0}),void 0===d?null:c[d]};return{getPattern:d,patterns:c,matchers:b}}var c={persistent:!1,repeat:!1,placeholder:" "},d={9:/[0-9]/,a:/[A-Za-z]/,"*":/[A-Za-z0-9]/};a.addInptType=function(a,b){d[a]=b},a.prototype.resetPattern=function(a){this.opts.patterns=a?this._specFromSinglePattern(a):this.opts.patterns,this.sel=j.get(this.el),this.val=this.el.value,this.delta=0,this._removeChars(),this.patternMatcher=b(this.opts.patterns);var c=this.patternMatcher.getPattern(this.val);this.mLength=c.mLength,this.chars=c.chars,this.inpts=c.inpts,this._processKey("",!1,!0)},a.prototype._updatePattern=function(){var a=this.patternMatcher.getPattern(this.val);a&&(this.mLength=a.mLength,this.chars=a.chars,this.inpts=a.inpts)},a.prototype._keyDown=function(a){var b=a.which||a.keyCode;return b&&k.isDelKey(b)?(this._processKey(null,b),k.preventDefault(a)):void 0},a.prototype._keyPress=function(a){var b,c;return a.which?b=a.which:(b=a.keyCode,c=k.isSpecialKey(b)),k.isDelKey(b)||c||k.isModifier(a)?void 0:(this._processKey(String.fromCharCode(b),!1),k.preventDefault(a))},a.prototype._paste=function(a){return this._processKey(k.getClip(a),!1),k.preventDefault(a)},a.prototype._focus=function(){var a=this;setTimeout(function(){var b=j.get(a.el),c=b.end>a.focus,d=0===b.end;(c||d)&&j.set(a.el,a.focus)},0)},a.prototype._processKey=function(a,b,c){if(this.sel=j.get(this.el),this.val=this.el.value,this.delta=0,this.sel.begin!==this.sel.end)this.delta=-1*Math.abs(this.sel.begin-this.sel.end),this.val=k.removeChars(this.val,this.sel.begin,this.sel.end);else if(b&&46==b)this._delete();else if(b&&this.sel.begin-1>=0)this.val=k.removeChars(this.val,this.sel.end-1,this.sel.end),this.delta=-1;else if(b)return!0;b||(this.val=k.addChars(this.val,a,this.sel.begin),this.delta+=a.length),this._formatValue(c)},a.prototype._delete=function(){for(;this.chars[this.sel.begin];)this._nextPos();this.sel.begin<this.val.length&&(this._nextPos(),this.val=k.removeChars(this.val,this.sel.end-1,this.sel.end),this.delta=-1)},a.prototype._nextPos=function(){this.sel.end++,this.sel.begin++},a.prototype._formatValue=function(a){this.newPos=this.sel.end+this.delta,this._removeChars(),this._updatePattern(),this._validateInpts(),this._addChars(),this.el.value=this.val.substr(0,this.mLength),("undefined"==typeof a||a===!1)&&j.set(this.el,this.newPos)},a.prototype._removeChars=function(){this.sel.end>this.focus&&(this.delta+=this.sel.end-this.focus);for(var a=0,b=0;b<=this.mLength;b++){var c,d=this.chars[b],e=this.hldrs[b],f=b+a;f=b>=this.sel.begin?f+this.delta:f,c=this.val.charAt(f),(d&&d==c||e&&e==c)&&(this.val=k.removeChars(this.val,f,f+1),a--)}this.hldrs={},this.focus=this.val.length},a.prototype._validateInpts=function(){for(var a=0;a<this.val.length;a++){var b=this.inpts[a],c=!d[b],e=!c&&!d[b].test(this.val.charAt(a)),f=this.inpts[a];(c||e)&&f&&(this.val=k.removeChars(this.val,a,a+1),this.focusStart--,this.newPos--,this.delta--,a--)}},a.prototype._addChars=function(){if(this.opts.persistent){for(var a=0;a<=this.mLength;a++)this.val.charAt(a)||(this.val=k.addChars(this.val,this.opts.placeholder,a),this.hldrs[a]=this.opts.placeholder),this._addChar(a);for(;this.chars[this.focus];)this.focus++}else for(var b=0;b<=this.val.length;b++){if(this.delta<=0&&b==this.focus)return!0;this._addChar(b)}},a.prototype._addChar=function(a){var b=this.chars[a];return b?(k.isBetween(a,[this.sel.begin-1,this.newPos+1])&&(this.newPos++,this.delta++),a<=this.focus&&this.focus++,this.hldrs[a]&&(delete this.hldrs[a],this.hldrs[a+1]=this.opts.placeholder),void(this.val=k.addChars(this.val,b,a))):!0},a.prototype._specFromSinglePattern=function(a){return[{"*":a}]};var e={},f=4,g=new RegExp("{{([^}]+)}}","g"),h=function(a){for(var b,c=[];b=g.exec(a);)c.push(b);return c};e.parse=function(a){var b={inpts:{},chars:{}},c=h(a),d=a.length,e=0,g=0,i=0,j=function(a){for(var c=a.length,d=0;c>d;d++)b.inpts[g]=a.charAt(d),g++;e++,i+=a.length+f-1};for(i;d>i;i++)i==c[e].index?j(c[e][1]):b.chars[i-e*f]=a.charAt(i);return b.mLength=i-e*f,b};var i=function(a){return"*"===a?/.*/:new RegExp(a)},j={};j.get=function(a){if("number"==typeof a.selectionStart)return{begin:a.selectionStart,end:a.selectionEnd};var b=document.selection.createRange();if(b&&b.parentElement()==a){var c=a.createTextRange(),d=a.createTextRange(),e=a.value.length;return c.moveToBookmark(b.getBookmark()),d.collapse(!1),c.compareEndPoints("StartToEnd",d)>-1?{begin:e,end:e}:{begin:-c.moveStart("character",-e),end:-c.moveEnd("character",-e)}}return{begin:0,end:0}},j.set=function(a,b){if(a.setSelectionRange)a.focus(),a.setSelectionRange(b,b);else if(a.createTextRange){var c=a.createTextRange();c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",b),c.select()}};var k={},l="undefined"!=typeof navigator?navigator.userAgent:null,m=/iphone/i.test(l);return k.extend=function(a){for(var b=1;b<arguments.length;b++)for(var c in arguments[b])a[c]=arguments[b][c];return a},k.addChars=function(a,b,c){return a.substr(0,c)+b+a.substr(c,a.length)},k.removeChars=function(a,b,c){return a.substr(0,b)+a.substr(c,a.length)},k.isBetween=function(a,b){return b.sort(function(a,b){return a-b}),a>b[0]&&a<b[1]},k.addListener=function(a,b,c){return"undefined"!=typeof a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent("on"+b,c)},k.preventDefault=function(a){return a.preventDefault?a.preventDefault():a.returnValue=!1},k.getClip=function(a){return a.clipboardData?a.clipboardData.getData("Text"):window.clipboardData?window.clipboardData.getData("Text"):void 0},k.isDelKey=function(a){return 8===a||46===a||m&&127===a},k.isSpecialKey=function(a){var b={9:"tab",13:"enter",35:"end",36:"home",37:"leftarrow",38:"uparrow",39:"rightarrow",40:"downarrow",116:"F5"};return b[a]},k.isModifier=function(a){return a.ctrlKey||a.altKey||a.metaKey},k.forEach=function(a,b,c){if(a.hasOwnProperty("length"))for(var d=0,e=a.length;e>d&&b.call(c,a[d],d,a)!==!1;d++);else for(var f in a)if(a.hasOwnProperty(f)&&b.call(c,a[f],f,a)===!1)break},a});
\ No newline at end of file
+!function(a,b,c){"undefined"!=typeof module&&module.exports?module.exports=c():"function"==typeof define&&define.amd?define(c):b[a]=c()}("Formatter",this,function(){function a(a,d){var e=this;if(e.el=a,!e.el)throw new TypeError("Must provide an existing element");if(e.opts=k.extend({},c,d),"undefined"!=typeof e.opts.pattern&&(e.opts.patterns=e._specFromSinglePattern(e.opts.pattern),delete e.opts.pattern),"undefined"==typeof e.opts.patterns)throw new TypeError("Must provide a pattern or array of patterns");e.patternMatcher=b(e.opts.patterns),e._updatePattern(),e.hldrs={},e.focus=0,k.addListener(e.el,"keydown",function(a){e._keyDown(a)}),k.addListener(e.el,"keypress",function(a){e._keyPress(a)}),k.addListener(e.el,"paste",function(a){e._paste(a)}),e.opts.persistent&&(e._processKey("",!1),e.el.blur(),k.addListener(e.el,"focus",function(a){e._focus(a)}),k.addListener(e.el,"click",function(a){e._focus(a)}),k.addListener(e.el,"touchstart",function(a){e._focus(a)}))}function b(a){var b=[],c=[];k.forEach(a,function(a){k.forEach(a,function(a,d){var f=e.parse(a),g=i(d);return b.push(g),c.push(f),!1})});var d=function(a){var d;return k.forEach(b,function(b,c){return b.test(a)?(d=c,!1):void 0}),void 0===d?null:c[d]};return{getPattern:d,patterns:c,matchers:b}}var c={persistent:!1,repeat:!1,placeholder:" "},d={9:/[0-9]/,a:/[A-Za-z]/,"*":/[A-Za-z0-9]/};a.addInptType=function(a,b){d[a]=b},a.prototype.resetPattern=function(a){this.opts.patterns=a?this._specFromSinglePattern(a):this.opts.patterns,this.sel=j.get(this.el),this.val=this.el.value,this.delta=0,this._removeChars(),this.patternMatcher=b(this.opts.patterns);var c=this.patternMatcher.getPattern(this.val);this.mLength=c.mLength,this.chars=c.chars,this.inpts=c.inpts,this._processKey("",!1,!0)},a.prototype._updatePattern=function(){var a=this.patternMatcher.getPattern(this.val);a&&(this.mLength=a.mLength,this.chars=a.chars,this.inpts=a.inpts)},a.prototype._keyDown=function(a){var b=a.which||a.keyCode;return b&&k.isDelKey(b)?(this._processKey(null,b),k.preventDefault(a)):void 0},a.prototype._keyPress=function(a){var b,c;return a.which?b=a.which:(b=a.keyCode,c=k.isSpecialKey(b)),k.isDelKey(b)||c||k.isModifier(a)?void 0:(this._processKey(String.fromCharCode(b),!1),k.preventDefault(a))},a.prototype._paste=function(a){return this._processKey(k.getClip(a),!1),k.preventDefault(a)},a.prototype._focus=function(){var a=this;setTimeout(function(){var b=j.get(a.el),c=b.end>a.focus,d=0===b.end;(c||d)&&j.set(a.el,a.focus)},0)},a.prototype._processKey=function(a,b,c){if(this.sel=j.get(this.el),this.val=this.el.value,this.delta=0,this.sel.begin!==this.sel.end)this.delta=-1*Math.abs(this.sel.begin-this.sel.end),this.val=k.removeChars(this.val,this.sel.begin,this.sel.end);else if(b&&46==b)this._delete();else if(b&&this.sel.begin-1>=0)this.val=k.removeChars(this.val,this.sel.end-1,this.sel.end),this.delta=-1;else if(b)return!0;b||(this.val=k.addChars(this.val,a,this.sel.begin),this.delta+=a.length),this._formatValue(c)},a.prototype._delete=function(){for(;this.chars[this.sel.begin];)this._nextPos();this.sel.begin<this.val.length&&(this._nextPos(),this.val=k.removeChars(this.val,this.sel.end-1,this.sel.end),this.delta=-1)},a.prototype._nextPos=function(){this.sel.end++,this.sel.begin++},a.prototype._formatValue=function(a){this.newPos=this.sel.end+this.delta,this._removeChars(),this._updatePattern(),this._validateInpts(),this._addChars(),this.el.value=this.val.substr(0,this.mLength),("undefined"==typeof a||a===!1)&&j.set(this.el,this.newPos)},a.prototype._removeChars=function(){this.sel.end>this.focus&&(this.delta+=this.sel.end-this.focus);for(var a=0,b=0;b<=this.mLength;b++){var c,d=this.chars[b],e=this.hldrs[b],f=b+a;f=b>=this.sel.begin?f+this.delta:f,c=this.val.charAt(f),(d&&d==c||e&&e==c)&&(this.val=k.removeChars(this.val,f,f+1),a--)}this.hldrs={},this.focus=this.val.length},a.prototype._validateInpts=function(){for(var a=0;a<this.val.length;a++){var b=this.inpts[a],c=!d[b],e=!c&&!d[b].test(this.val.charAt(a)),f=this.inpts[a];(c||e)&&f&&(this.val=k.removeChars(this.val,a,a+1),this.focusStart--,this.newPos--,this.delta--,a--)}},a.prototype._addChars=function(){if(this.opts.persistent){for(var a=0;a<=this.mLength;a++)this.val.charAt(a)||(this.val=k.addChars(this.val,this.opts.placeholder,a),this.hldrs[a]=this.opts.placeholder),this._addChar(a);for(;this.chars[this.focus];)this.focus++}else for(var b=0;b<=this.val.length;b++){if(this.delta<=0&&b==this.focus)return!0;this._addChar(b)}},a.prototype._addChar=function(a){var b=this.chars[a];return b?(k.isBetween(a,[this.sel.begin-1,this.newPos+1])&&(this.newPos++,this.delta++),a<=this.focus&&this.focus++,this.hldrs[a]&&(delete this.hldrs[a],this.hldrs[a+1]=this.opts.placeholder),void(this.val=k.addChars(this.val,b,a))):!0},a.prototype._specFromSinglePattern=function(a){return[{"*":a}]};var e={},f=4,g=new RegExp("{{([^}]+)}}","g"),h=function(a){for(var b,c=[];b=g.exec(a);)c.push(b);return c};e.parse=function(a){var b={inpts:{},chars:{}},c=h(a),d=a.length,e=0,g=0,i=0,j=function(a){for(var c=a.length,d=0;c>d;d++)b.inpts[g]=a.charAt(d),g++;e++,i+=a.length+f-1};for(i;d>i;i++)e<c.length&&i==c[e].index?j(c[e][1]):b.chars[i-e*f]=a.charAt(i);return b.mLength=i-e*f,b};var i=function(a){return"*"===a?/.*/:new RegExp(a)},j={};j.get=function(a){if("number"==typeof a.selectionStart)return{begin:a.selectionStart,end:a.selectionEnd};var b=document.selection.createRange();if(b&&b.parentElement()==a){var c=a.createTextRange(),d=a.createTextRange(),e=a.value.length;return c.moveToBookmark(b.getBookmark()),d.collapse(!1),c.compareEndPoints("StartToEnd",d)>-1?{begin:e,end:e}:{begin:-c.moveStart("character",-e),end:-c.moveEnd("character",-e)}}return{begin:0,end:0}},j.set=function(a,b){if(a.setSelectionRange)a.focus(),a.setSelectionRange(b,b);else if(a.createTextRange){var c=a.createTextRange();c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",b),c.select()}};var k={},l="undefined"!=typeof navigator?navigator.userAgent:null,m=/iphone/i.test(l);return k.extend=function(a){for(var b=1;b<arguments.length;b++)for(var c in arguments[b])a[c]=arguments[b][c];return a},k.addChars=function(a,b,c){return a.substr(0,c)+b+a.substr(c,a.length)},k.removeChars=function(a,b,c){return a.substr(0,b)+a.substr(c,a.length)},k.isBetween=function(a,b){return b.sort(function(a,b){return a-b}),a>b[0]&&a<b[1]},k.addListener=function(a,b,c){return"undefined"!=typeof a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent("on"+b,c)},k.preventDefault=function(a){return a.preventDefault?a.preventDefault():a.returnValue=!1},k.getClip=function(a){return a.clipboardData?a.clipboardData.getData("Text"):window.clipboardData?window.clipboardData.getData("Text"):void 0},k.isDelKey=function(a){return 8===a||46===a||m&&127===a},k.isSpecialKey=function(a){var b={9:"tab",13:"enter",35:"end",36:"home",37:"leftarrow",38:"uparrow",39:"rightarrow",40:"downarrow",116:"F5"};return b[a]},k.isModifier=function(a){return a.ctrlKey||a.altKey||a.metaKey},k.forEach=function(a,b,c){if(a.hasOwnProperty("length"))for(var d=0,e=a.length;e>d&&b.call(c,a[d],d,a)!==!1;d++);else for(var f in a)if(a.hasOwnProperty(f)&&b.call(c,a[f],f,a)===!1)break},a});
\ No newline at end of file
diff --git a/lib/jquery.formatter.js b/lib/jquery.formatter.js
index 95b37dc..6619167 100644
--- a/lib/jquery.formatter.js
+++ b/lib/jquery.formatter.js
@@ -507,7 +507,7 @@ pattern.parse = function (pattern) {
 
   // Process match or add chars
   for (i; i < pLength; i++) {
-    if (i == matches[mCount].index) {
+    if (mCount < matches.length && i == matches[mCount].index) {
       processMatch(matches[mCount][1]);
     } else {
       info.chars[i - (mCount * DELIM_SIZE)] = pattern.charAt(i);
@@ -518,6 +518,7 @@ pattern.parse = function (pattern) {
   info.mLength = i - (mCount * DELIM_SIZE);
   return info;
 };
+
 //
 // Parse a matcher string into a RegExp. Accepts valid regular
 // expressions and the catchall '*'.
diff --git a/lib/jquery.formatter.min.js b/lib/jquery.formatter.min.js
index ab25322..9dcd970 100644
--- a/lib/jquery.formatter.min.js
+++ b/lib/jquery.formatter.min.js
@@ -6,4 +6,4 @@
  * thanks to digitalBush/jquery.maskedinput for some of the trickier
  * keycode handling
  */
-!function(a,b,c,d){function e(a,b){var c=this;if(c.el=a,!c.el)throw new TypeError("Must provide an existing element");if(c.opts=o.extend({},g,b),"undefined"!=typeof c.opts.pattern&&(c.opts.patterns=c._specFromSinglePattern(c.opts.pattern),delete c.opts.pattern),"undefined"==typeof c.opts.patterns)throw new TypeError("Must provide a pattern or array of patterns");c.patternMatcher=f(c.opts.patterns),c._updatePattern(),c.hldrs={},c.focus=0,o.addListener(c.el,"keydown",function(a){c._keyDown(a)}),o.addListener(c.el,"keypress",function(a){c._keyPress(a)}),o.addListener(c.el,"paste",function(a){c._paste(a)}),c.opts.persistent&&(c._processKey("",!1),c.el.blur(),o.addListener(c.el,"focus",function(a){c._focus(a)}),o.addListener(c.el,"click",function(a){c._focus(a)}),o.addListener(c.el,"touchstart",function(a){c._focus(a)}))}function f(a){var b=[],c=[];o.forEach(a,function(a){o.forEach(a,function(a,d){var e=i.parse(a),f=m(d);return b.push(f),c.push(e),!1})});var e=function(a){var e;return o.forEach(b,function(b,c){return b.test(a)?(e=c,!1):void 0}),e===d?null:c[e]};return{getPattern:e,patterns:c,matchers:b}}var g={persistent:!1,repeat:!1,placeholder:" "},h={9:/[0-9]/,a:/[A-Za-z]/,"*":/[A-Za-z0-9]/};e.addInptType=function(a,b){h[a]=b},e.prototype.resetPattern=function(a){this.opts.patterns=a?this._specFromSinglePattern(a):this.opts.patterns,this.sel=n.get(this.el),this.val=this.el.value,this.delta=0,this._removeChars(),this.patternMatcher=f(this.opts.patterns);var b=this.patternMatcher.getPattern(this.val);this.mLength=b.mLength,this.chars=b.chars,this.inpts=b.inpts,this._processKey("",!1,!0)},e.prototype._updatePattern=function(){var a=this.patternMatcher.getPattern(this.val);a&&(this.mLength=a.mLength,this.chars=a.chars,this.inpts=a.inpts)},e.prototype._keyDown=function(a){var b=a.which||a.keyCode;return b&&o.isDelKey(b)?(this._processKey(null,b),o.preventDefault(a)):void 0},e.prototype._keyPress=function(a){var b,c;return a.which?b=a.which:(b=a.keyCode,c=o.isSpecialKey(b)),o.isDelKey(b)||c||o.isModifier(a)?void 0:(this._processKey(String.fromCharCode(b),!1),o.preventDefault(a))},e.prototype._paste=function(a){return this._processKey(o.getClip(a),!1),o.preventDefault(a)},e.prototype._focus=function(){var a=this;setTimeout(function(){var b=n.get(a.el),c=b.end>a.focus,d=0===b.end;(c||d)&&n.set(a.el,a.focus)},0)},e.prototype._processKey=function(a,b,c){if(this.sel=n.get(this.el),this.val=this.el.value,this.delta=0,this.sel.begin!==this.sel.end)this.delta=-1*Math.abs(this.sel.begin-this.sel.end),this.val=o.removeChars(this.val,this.sel.begin,this.sel.end);else if(b&&46==b)this._delete();else if(b&&this.sel.begin-1>=0)this.val=o.removeChars(this.val,this.sel.end-1,this.sel.end),this.delta=-1;else if(b)return!0;b||(this.val=o.addChars(this.val,a,this.sel.begin),this.delta+=a.length),this._formatValue(c)},e.prototype._delete=function(){for(;this.chars[this.sel.begin];)this._nextPos();this.sel.begin<this.val.length&&(this._nextPos(),this.val=o.removeChars(this.val,this.sel.end-1,this.sel.end),this.delta=-1)},e.prototype._nextPos=function(){this.sel.end++,this.sel.begin++},e.prototype._formatValue=function(a){this.newPos=this.sel.end+this.delta,this._removeChars(),this._updatePattern(),this._validateInpts(),this._addChars(),this.el.value=this.val.substr(0,this.mLength),("undefined"==typeof a||a===!1)&&n.set(this.el,this.newPos)},e.prototype._removeChars=function(){this.sel.end>this.focus&&(this.delta+=this.sel.end-this.focus);for(var a=0,b=0;b<=this.mLength;b++){var c,d=this.chars[b],e=this.hldrs[b],f=b+a;f=b>=this.sel.begin?f+this.delta:f,c=this.val.charAt(f),(d&&d==c||e&&e==c)&&(this.val=o.removeChars(this.val,f,f+1),a--)}this.hldrs={},this.focus=this.val.length},e.prototype._validateInpts=function(){for(var a=0;a<this.val.length;a++){var b=this.inpts[a],c=!h[b],d=!c&&!h[b].test(this.val.charAt(a)),e=this.inpts[a];(c||d)&&e&&(this.val=o.removeChars(this.val,a,a+1),this.focusStart--,this.newPos--,this.delta--,a--)}},e.prototype._addChars=function(){if(this.opts.persistent){for(var a=0;a<=this.mLength;a++)this.val.charAt(a)||(this.val=o.addChars(this.val,this.opts.placeholder,a),this.hldrs[a]=this.opts.placeholder),this._addChar(a);for(;this.chars[this.focus];)this.focus++}else for(var b=0;b<=this.val.length;b++){if(this.delta<=0&&b==this.focus)return!0;this._addChar(b)}},e.prototype._addChar=function(a){var b=this.chars[a];return b?(o.isBetween(a,[this.sel.begin-1,this.newPos+1])&&(this.newPos++,this.delta++),a<=this.focus&&this.focus++,this.hldrs[a]&&(delete this.hldrs[a],this.hldrs[a+1]=this.opts.placeholder),void(this.val=o.addChars(this.val,b,a))):!0},e.prototype._specFromSinglePattern=function(a){return[{"*":a}]};var i={},j=4,k=new RegExp("{{([^}]+)}}","g"),l=function(a){for(var b,c=[];b=k.exec(a);)c.push(b);return c};i.parse=function(a){var b={inpts:{},chars:{}},c=l(a),d=a.length,e=0,f=0,g=0,h=function(a){for(var c=a.length,d=0;c>d;d++)b.inpts[f]=a.charAt(d),f++;e++,g+=a.length+j-1};for(g;d>g;g++)g==c[e].index?h(c[e][1]):b.chars[g-e*j]=a.charAt(g);return b.mLength=g-e*j,b};var m=function(a){return"*"===a?/.*/:new RegExp(a)},n={};n.get=function(a){if("number"==typeof a.selectionStart)return{begin:a.selectionStart,end:a.selectionEnd};var b=c.selection.createRange();if(b&&b.parentElement()==a){var d=a.createTextRange(),e=a.createTextRange(),f=a.value.length;return d.moveToBookmark(b.getBookmark()),e.collapse(!1),d.compareEndPoints("StartToEnd",e)>-1?{begin:f,end:f}:{begin:-d.moveStart("character",-f),end:-d.moveEnd("character",-f)}}return{begin:0,end:0}},n.set=function(a,b){if(a.setSelectionRange)a.focus(),a.setSelectionRange(b,b);else if(a.createTextRange){var c=a.createTextRange();c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",b),c.select()}};var o={},p="undefined"!=typeof navigator?navigator.userAgent:null,q=/iphone/i.test(p);o.extend=function(a){for(var b=1;b<arguments.length;b++)for(var c in arguments[b])a[c]=arguments[b][c];return a},o.addChars=function(a,b,c){return a.substr(0,c)+b+a.substr(c,a.length)},o.removeChars=function(a,b,c){return a.substr(0,b)+a.substr(c,a.length)},o.isBetween=function(a,b){return b.sort(function(a,b){return a-b}),a>b[0]&&a<b[1]},o.addListener=function(a,b,c){return"undefined"!=typeof a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent("on"+b,c)},o.preventDefault=function(a){return a.preventDefault?a.preventDefault():a.returnValue=!1},o.getClip=function(a){return a.clipboardData?a.clipboardData.getData("Text"):b.clipboardData?b.clipboardData.getData("Text"):void 0},o.isDelKey=function(a){return 8===a||46===a||q&&127===a},o.isSpecialKey=function(a){var b={9:"tab",13:"enter",35:"end",36:"home",37:"leftarrow",38:"uparrow",39:"rightarrow",40:"downarrow",116:"F5"};return b[a]},o.isModifier=function(a){return a.ctrlKey||a.altKey||a.metaKey},o.forEach=function(a,b,c){if(a.hasOwnProperty("length"))for(var d=0,e=a.length;e>d&&b.call(c,a[d],d,a)!==!1;d++);else for(var f in a)if(a.hasOwnProperty(f)&&b.call(c,a[f],f,a)===!1)break};var r="formatter";a.fn[r]=function(b){return"object"==typeof b&&this.each(function(){a.data(this,"plugin_"+r)||a.data(this,"plugin_"+r,new e(this,b))}),this.resetPattern=function(b){return this.each(function(){var c=a.data(this,"plugin_"+r);c&&c.resetPattern(b)}),this},this},a.fn[r].addInptType=function(a,b){e.addInptType(a,b)}}(jQuery,window,document);
\ No newline at end of file
+!function(a,b,c,d){function e(a,b){var c=this;if(c.el=a,!c.el)throw new TypeError("Must provide an existing element");if(c.opts=o.extend({},g,b),"undefined"!=typeof c.opts.pattern&&(c.opts.patterns=c._specFromSinglePattern(c.opts.pattern),delete c.opts.pattern),"undefined"==typeof c.opts.patterns)throw new TypeError("Must provide a pattern or array of patterns");c.patternMatcher=f(c.opts.patterns),c._updatePattern(),c.hldrs={},c.focus=0,o.addListener(c.el,"keydown",function(a){c._keyDown(a)}),o.addListener(c.el,"keypress",function(a){c._keyPress(a)}),o.addListener(c.el,"paste",function(a){c._paste(a)}),c.opts.persistent&&(c._processKey("",!1),c.el.blur(),o.addListener(c.el,"focus",function(a){c._focus(a)}),o.addListener(c.el,"click",function(a){c._focus(a)}),o.addListener(c.el,"touchstart",function(a){c._focus(a)}))}function f(a){var b=[],c=[];o.forEach(a,function(a){o.forEach(a,function(a,d){var e=i.parse(a),f=m(d);return b.push(f),c.push(e),!1})});var e=function(a){var e;return o.forEach(b,function(b,c){return b.test(a)?(e=c,!1):void 0}),e===d?null:c[e]};return{getPattern:e,patterns:c,matchers:b}}var g={persistent:!1,repeat:!1,placeholder:" "},h={9:/[0-9]/,a:/[A-Za-z]/,"*":/[A-Za-z0-9]/};e.addInptType=function(a,b){h[a]=b},e.prototype.resetPattern=function(a){this.opts.patterns=a?this._specFromSinglePattern(a):this.opts.patterns,this.sel=n.get(this.el),this.val=this.el.value,this.delta=0,this._removeChars(),this.patternMatcher=f(this.opts.patterns);var b=this.patternMatcher.getPattern(this.val);this.mLength=b.mLength,this.chars=b.chars,this.inpts=b.inpts,this._processKey("",!1,!0)},e.prototype._updatePattern=function(){var a=this.patternMatcher.getPattern(this.val);a&&(this.mLength=a.mLength,this.chars=a.chars,this.inpts=a.inpts)},e.prototype._keyDown=function(a){var b=a.which||a.keyCode;return b&&o.isDelKey(b)?(this._processKey(null,b),o.preventDefault(a)):void 0},e.prototype._keyPress=function(a){var b,c;return a.which?b=a.which:(b=a.keyCode,c=o.isSpecialKey(b)),o.isDelKey(b)||c||o.isModifier(a)?void 0:(this._processKey(String.fromCharCode(b),!1),o.preventDefault(a))},e.prototype._paste=function(a){return this._processKey(o.getClip(a),!1),o.preventDefault(a)},e.prototype._focus=function(){var a=this;setTimeout(function(){var b=n.get(a.el),c=b.end>a.focus,d=0===b.end;(c||d)&&n.set(a.el,a.focus)},0)},e.prototype._processKey=function(a,b,c){if(this.sel=n.get(this.el),this.val=this.el.value,this.delta=0,this.sel.begin!==this.sel.end)this.delta=-1*Math.abs(this.sel.begin-this.sel.end),this.val=o.removeChars(this.val,this.sel.begin,this.sel.end);else if(b&&46==b)this._delete();else if(b&&this.sel.begin-1>=0)this.val=o.removeChars(this.val,this.sel.end-1,this.sel.end),this.delta=-1;else if(b)return!0;b||(this.val=o.addChars(this.val,a,this.sel.begin),this.delta+=a.length),this._formatValue(c)},e.prototype._delete=function(){for(;this.chars[this.sel.begin];)this._nextPos();this.sel.begin<this.val.length&&(this._nextPos(),this.val=o.removeChars(this.val,this.sel.end-1,this.sel.end),this.delta=-1)},e.prototype._nextPos=function(){this.sel.end++,this.sel.begin++},e.prototype._formatValue=function(a){this.newPos=this.sel.end+this.delta,this._removeChars(),this._updatePattern(),this._validateInpts(),this._addChars(),this.el.value=this.val.substr(0,this.mLength),("undefined"==typeof a||a===!1)&&n.set(this.el,this.newPos)},e.prototype._removeChars=function(){this.sel.end>this.focus&&(this.delta+=this.sel.end-this.focus);for(var a=0,b=0;b<=this.mLength;b++){var c,d=this.chars[b],e=this.hldrs[b],f=b+a;f=b>=this.sel.begin?f+this.delta:f,c=this.val.charAt(f),(d&&d==c||e&&e==c)&&(this.val=o.removeChars(this.val,f,f+1),a--)}this.hldrs={},this.focus=this.val.length},e.prototype._validateInpts=function(){for(var a=0;a<this.val.length;a++){var b=this.inpts[a],c=!h[b],d=!c&&!h[b].test(this.val.charAt(a)),e=this.inpts[a];(c||d)&&e&&(this.val=o.removeChars(this.val,a,a+1),this.focusStart--,this.newPos--,this.delta--,a--)}},e.prototype._addChars=function(){if(this.opts.persistent){for(var a=0;a<=this.mLength;a++)this.val.charAt(a)||(this.val=o.addChars(this.val,this.opts.placeholder,a),this.hldrs[a]=this.opts.placeholder),this._addChar(a);for(;this.chars[this.focus];)this.focus++}else for(var b=0;b<=this.val.length;b++){if(this.delta<=0&&b==this.focus)return!0;this._addChar(b)}},e.prototype._addChar=function(a){var b=this.chars[a];return b?(o.isBetween(a,[this.sel.begin-1,this.newPos+1])&&(this.newPos++,this.delta++),a<=this.focus&&this.focus++,this.hldrs[a]&&(delete this.hldrs[a],this.hldrs[a+1]=this.opts.placeholder),void(this.val=o.addChars(this.val,b,a))):!0},e.prototype._specFromSinglePattern=function(a){return[{"*":a}]};var i={},j=4,k=new RegExp("{{([^}]+)}}","g"),l=function(a){for(var b,c=[];b=k.exec(a);)c.push(b);return c};i.parse=function(a){var b={inpts:{},chars:{}},c=l(a),d=a.length,e=0,f=0,g=0,h=function(a){for(var c=a.length,d=0;c>d;d++)b.inpts[f]=a.charAt(d),f++;e++,g+=a.length+j-1};for(g;d>g;g++)e<c.length&&g==c[e].index?h(c[e][1]):b.chars[g-e*j]=a.charAt(g);return b.mLength=g-e*j,b};var m=function(a){return"*"===a?/.*/:new RegExp(a)},n={};n.get=function(a){if("number"==typeof a.selectionStart)return{begin:a.selectionStart,end:a.selectionEnd};var b=c.selection.createRange();if(b&&b.parentElement()==a){var d=a.createTextRange(),e=a.createTextRange(),f=a.value.length;return d.moveToBookmark(b.getBookmark()),e.collapse(!1),d.compareEndPoints("StartToEnd",e)>-1?{begin:f,end:f}:{begin:-d.moveStart("character",-f),end:-d.moveEnd("character",-f)}}return{begin:0,end:0}},n.set=function(a,b){if(a.setSelectionRange)a.focus(),a.setSelectionRange(b,b);else if(a.createTextRange){var c=a.createTextRange();c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",b),c.select()}};var o={},p="undefined"!=typeof navigator?navigator.userAgent:null,q=/iphone/i.test(p);o.extend=function(a){for(var b=1;b<arguments.length;b++)for(var c in arguments[b])a[c]=arguments[b][c];return a},o.addChars=function(a,b,c){return a.substr(0,c)+b+a.substr(c,a.length)},o.removeChars=function(a,b,c){return a.substr(0,b)+a.substr(c,a.length)},o.isBetween=function(a,b){return b.sort(function(a,b){return a-b}),a>b[0]&&a<b[1]},o.addListener=function(a,b,c){return"undefined"!=typeof a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent("on"+b,c)},o.preventDefault=function(a){return a.preventDefault?a.preventDefault():a.returnValue=!1},o.getClip=function(a){return a.clipboardData?a.clipboardData.getData("Text"):b.clipboardData?b.clipboardData.getData("Text"):void 0},o.isDelKey=function(a){return 8===a||46===a||q&&127===a},o.isSpecialKey=function(a){var b={9:"tab",13:"enter",35:"end",36:"home",37:"leftarrow",38:"uparrow",39:"rightarrow",40:"downarrow",116:"F5"};return b[a]},o.isModifier=function(a){return a.ctrlKey||a.altKey||a.metaKey},o.forEach=function(a,b,c){if(a.hasOwnProperty("length"))for(var d=0,e=a.length;e>d&&b.call(c,a[d],d,a)!==!1;d++);else for(var f in a)if(a.hasOwnProperty(f)&&b.call(c,a[f],f,a)===!1)break};var r="formatter";a.fn[r]=function(b){return"object"==typeof b&&this.each(function(){a.data(this,"plugin_"+r)||a.data(this,"plugin_"+r,new e(this,b))}),this.resetPattern=function(b){return this.each(function(){var c=a.data(this,"plugin_"+r);c&&c.resetPattern(b)}),this},this},a.fn[r].addInptType=function(a,b){e.addInptType(a,b)}}(jQuery,window,document);
\ No newline at end of file
diff --git a/src/pattern.js b/src/pattern.js
index a21233e..5463b66 100644
--- a/src/pattern.js
+++ b/src/pattern.js
@@ -58,7 +58,7 @@ pattern.parse = function (pattern) {
 
   // Process match or add chars
   for (i; i < pLength; i++) {
-    if (i == matches[mCount].index) {
+    if (mCount < matches.length && i == matches[mCount].index) {
       processMatch(matches[mCount][1]);
     } else {
       info.chars[i - (mCount * DELIM_SIZE)] = pattern.charAt(i);
@@ -68,4 +68,4 @@ pattern.parse = function (pattern) {
   // Set mLength and return
   info.mLength = i - (mCount * DELIM_SIZE);
   return info;
-};
\ No newline at end of file
+};
diff --git a/test/pattern.js b/test/pattern.js
index 7d736e1..4fdac9f 100644
--- a/test/pattern.js
+++ b/test/pattern.js
@@ -4,7 +4,7 @@
  * (C) 2013 First Opinion
  * MIT LICENCE
  *
- */ 
+ */
 
 // 3rd party
 var should = require('chai').should(),
@@ -45,6 +45,34 @@ describe('pattern.js', function () {
       });
       assert.equal(result.mLength, 14);
     });
+
+    describe('edge cases', function () {
+      it('Should parse a pattern with a leading char', function () {
+        var result = pattern.parse('_{{**}}');
+
+        assert.deepEqual(result.chars, {
+          '0': '_'
+        });
+        assert.deepEqual(result.inpts, {
+          '0': '*',
+          '1': '*'
+        });
+        assert.equal(result.mLength, 3);
+      });
+
+      it('Should parse a pattern with a trailing char', function () {
+        var result = pattern.parse('{{**}}_');
+
+        assert.deepEqual(result.chars, {
+          '2': '_'
+        });
+        assert.deepEqual(result.inpts, {
+          '0': '*',
+          '1': '*'
+        });
+        assert.equal(result.mLength, 3);
+      });
+    });
   });
 
-});
\ No newline at end of file
+});
