Skip to content

Commit

Permalink
Merge pull request #60 from joecorcoran/ruby-regex-fix-2
Browse files Browse the repository at this point in the history
Change \A and \z to ^ and $ in regex (with tests)
  • Loading branch information
jamesmk committed May 25, 2015
2 parents 834cf5c + 57ca6d4 commit 266bb5e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/assets/javascripts/judge.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
var convertRegExp = function(string) {
var parts = string.slice(1, -1).split(':'),
flags = parts.shift().replace('?', ''),
source = parts.join(':').replace(/\\\\/g, '\\');
source = parts.join(':').replace(/\\\\/g, '\\').replace('\\A', '^').replace('\\z', '$');
return new RegExp(source, convertFlags(flags));
};

Expand Down
20 changes: 20 additions & 0 deletions spec/javascripts/judge-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,26 @@ describe('judge', function() {
el.value = 'AbC';
expect(validator({ 'with': '(?-mix:[A-Za-z]+)' }, {})).toBeValid();
});

it('converts devise\'s Ruby email regex and returns invalid with invalid email', function() {
el.value = 'not an email';
expect(validator({ 'with': '(?-mix:\\A[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+\\z)' }, { invalid: 'is invalid' })).toBeInvalidWith(['is invalid']);
});

it('converts devise\'s Ruby email regex and returns valid with valid email', function() {
el.value = 'john.doe@somesite.com';
expect(validator({ 'with': '(?-mix:\\A[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+\\z)' }, {})).toBeValid();
});

it('converts a Ruby slug regex and returns invalid with invalid slug', function() {
el.value = 'not an slug.';
expect(validator({ 'with': '(?-mix:\\A[-a-zA-Z0-9]+\\z)' }, { invalid: 'is invalid' })).toBeInvalidWith(['is invalid']);
});

it('converts a Ruby slug regex and returns valid with valid slug', function() {
el.value = 'this-is-a-slug';
expect(validator({ 'with': '(?-mix:\\A[-a-zA-Z0-9]+\\z)' }, {})).toBeValid();
});
});

describe('without', function() {
Expand Down

0 comments on commit 266bb5e

Please sign in to comment.