Skip to content

Instantly share code, notes, and snippets.

@the-vampiire
Created May 4, 2017 00:36
Show Gist options
  • Select an option

  • Save the-vampiire/6022704329fa1cfbe2a78c722f992648 to your computer and use it in GitHub Desktop.

Select an option

Save the-vampiire/6022704329fa1cfbe2a78c722f992648 to your computer and use it in GitHub Desktop.
By the Power of RegEx...
var map = {
"should return true." : '\n',
"should return false." : '\n',
"should return a boolean." : '\n',
"should return false" : '\n',
"should return true" : '\n',
"telephoneCheck(" : '',
'")' : '"',
'.' : '',
'"' : ''
};
var str = 'telephoneCheck("555-555-5555") should return a boolean.\
telephoneCheck("1 555-555-5555") should return true.\
telephoneCheck("1 (555) 555-5555") should return true. \
telephoneCheck("5555555555") should return true. \
telephoneCheck("555-555-5555") should return true. \
telephoneCheck("(555)555-5555") should return true. \
telephoneCheck("1(555)555-5555") should return true.\
telephoneCheck("555-5555") should return false.\
telephoneCheck("5555555") should return false.\
telephoneCheck("1 555)555-5555") should return false.\
telephoneCheck("1 555 555 5555") should return true.\
telephoneCheck("1 456 789 4444") should return true.\
telephoneCheck("123**&!!asdf#") should return false.\
telephoneCheck("55555555") should return false.\
telephoneCheck("(6505552368)") should return false\
telephoneCheck("2 (757) 622-7382") should return false.\
telephoneCheck("0 (757) 622-7382") should return false.\
telephoneCheck("-1 (757) 622-7382") should return false\
telephoneCheck("2 757 622-7382") should return false.\
telephoneCheck("10 (757) 622-7382") should return false.\
telephoneCheck("27576227382") should return false.\
telephoneCheck("(275)76227382") should return false.\
telephoneCheck("2(757)6227382") should return false.\
telephoneCheck("2(757)622-7382") should return false.\
telephoneCheck("555)-555-5555") should return false.\
telephoneCheck("(555-555-5555") should return false.\
telephoneCheck("(555)5(55?)-5555") should return false.';
var replaced = str.replace(/(")|("\))|(telephoneCheck\()|(should return true)|(should return false)|(should return a boolean.)|(\.)/g, function(m){return map[m];});
console.log(replaced);
/*
Results:
555-555-5555)
1 555-555-5555)
1 (555) 555-5555)
5555555555)
555-555-5555)
(555)555-5555)
1(555)555-5555)
555-5555)
5555555)
1 555)555-5555)
1 555 555 5555)
1 456 789 4444)
123**&!!asdf#)
55555555)
(6505552368))
2 (757) 622-7382)
0 (757) 622-7382)
-1 (757) 622-7382)
2 757 622-7382)
10 (757) 622-7382)
27576227382)
(275)76227382)
2(757)6227382)
2(757)622-7382)
555)-555-5555)
(555-555-5555)
(555)5(55?)-5555)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment