Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Regex object for checking the context of the hint |
| 3 | */ |
| 4 | define({ |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame^] | 5 | |
| 6 | /** |
| 7 | * Create analyzer based on regular expression. |
| 8 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 9 | create : function (regex) { |
| 10 | return Object.create(this)._init(regex); |
| 11 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame^] | 12 | |
| 13 | // Initialize analyzer |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 14 | _init : function (regex) { |
| 15 | try { |
| 16 | this._regex = new RegExp(regex); |
| 17 | } |
| 18 | catch (e) { |
| 19 | KorAP.log(0, e); |
| 20 | return; |
| 21 | }; |
| 22 | return this; |
| 23 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame^] | 24 | |
| 25 | /** |
| 26 | * Check a context based on the analyzer |
| 27 | * and return a valid context string. |
| 28 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 29 | test : function (text) { |
| 30 | if (!this._regex.exec(text)) |
| 31 | return; |
| 32 | return RegExp.$1; |
| 33 | } |
| 34 | }); |