Skip to content

Instantly share code, notes, and snippets.

@Coconuthack
Created April 28, 2013 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coconuthack/5476452 to your computer and use it in GitHub Desktop.
Save Coconuthack/5476452 to your computer and use it in GitHub Desktop.
Eloquent JS Ch10 Regular Expressions
//EloquentJS CH10 Regex
var slash = /\//;
show("AC/DC".search(slash)); //-> 2
var asteriskOrBrace = /[\{\*]/;//finds any of the '{' or '*' chars
var story =
"We noticed the *giant sloth*, hanging from a giant branch.";
show(story.search(asteriskOrBrace)); //-> 15
var digitSurroundedBySpace = /\s\d\s/;
show("1a 2 3d".search(digitSurroundedBySpace)); //-> 4
var notABC = /[^ABC]/;
show("ABCBACCBBADABC".search(notABC));//-> 10
//exercise. regex expression match date in format 'xx/xx/xxxx' where x's
//are digits
var dateformat = /\d\d\/\d\d\/\d\d\d\d/;
"born 15/11/2003 (mother Spot): White Fang".search(dateformat); //-> 5
/a+/.test("blah");//->true; matches any string that contains an 'a' char
/^a+$/.test("blah")//->false; matches pattern entirely of 'a' char
/cat/.test("concatenate"); //-> true
/\bcat\b/.test("concatenate")); //-> false ; matches 'bounded'cat'bounded'
var parenthesizedText = /\(.*\)/; //-> any charactersequence between ( )
"Its (the sloth's) claws were gigantic!".search(parenthesizedText);//-> 4
var datePattern = /\d{1,2}\/\d\d?\/\d{4}/;
"born 15/11/2003 (mother Spot): White Fang".search(datePattern); //-> 5
//exercise. pattern that matches e-mail addresses
/*Write a pattern that matches e-mail addresses. For simplicity, assume
that the parts before and after the @ can contain only alphanumeric
characters and the characters . and - (dot and dash), while the last
part of the address, the country code after the last dot, may only
contain alphanumeric characters, and must be two or three characters
long.*/
/[\w+\.\-]\@\W+\.\w{2,3}/ //my attempt
var answer = /\b[\w\.-]+@[\w\.-]+\.\w{2,3}\b/;
answer.test("kenny@test.net");//-> true
answer.test("I mailt kenny@tets.nets, but it didn wrok!")//-> false
var cartoonCrying = /boo(hoo+)+/i;
"Then, he exclaimed 'Boohoooohoohooo'".search(cartoonCrying)//-> 20
var holyCow = /(sacred|holy) (cow|bovine|bull|taurus)/i;
holyCow.test("Sacred bovine!"); //-?true
var parentz = "Heloom my name is (Dijon Kock Adu);"
parentz.match(/\((.*)\)/);//-> ["(Dijon Kock Adu)","Dijon Kock Adu"]
/* When given a string, this function looks for something that follows
the date format we saw earlier. If it can find such a date, it puts the
values into a Date object. Otherwise, it throws an exception. Make it
accept dates in which the day or month are written with only one digit.*/
function extractDate (string) {
// extracts date out of paragraph and returns date object using regex
pattern = /(\d\d?)\/(\d\d?)\/(\d{4})/;
var date = string.match(pattern); //-> ['date','day', 'month', 'year']
if(date!=null) {
return new Date(+date[3],+date[2]-1,+date[2]);
} else {
throw new Error("No date found in '"+ string + "'.");
}
}//answered pretty good
// Replace method example when second argument is a string - Neat Trick
function eatOne(match, amount, unit) {
amount = Number(amount) - 1;
if (amount == 1) {
unit = unit.slice(0, unit.length - 1);
}
else if (amount == 0) {
unit = unit + "s";
amount = "no";
}
return amount + " " + unit;
}
var stock = "1 lemon, 2 cabbages, and 101 eggs";
stock = stock.replace(/(\d+) (\w+)/g, eatOne);
print(stock);//-> no lemons, 1 cabbage, and 100 eggs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment