Last active
September 4, 2018 05:44
-
-
Save Anna-Myzukina/af41f2329f8d626ea21fdb7b677ea1f4 to your computer and use it in GitHub Desktop.
regex-adventure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 11 | |
/*Export a function that takes a string argument and returns whether the string starts with `cat`, `dog`, or `robot` followed by a number to the end of the string | |
*/ | |
module.exports = function (str) { | |
return /^(cat|dog|robot)\d+$/.test(str); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 3 | |
/*This time your regex should match only when the given string ends with | |
"BANANAS" | |
*/ | |
module.exports = function (str) { | |
return /BANANAS$/.test(str); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// exercise 2 | |
/*This time your regex should match only when the given string begins with | |
"LITERALLY" | |
*/ | |
module.exports = function (str) { | |
return /^LITERALLY/.test(str); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 14 | |
/*Implement a custom markdown operator that will translate all text between `@@...@@` and into `<blink>...</blink>` tags. | |
For example, `@@whatever@@` becomes `<blink>whatever</blink>`. | |
In addition to the `@@...@@` operator, your code should support existing markdown syntax, including inside `@@...@@` operators. | |
Your code should accept a string of markdown as input and return a string of html output | |
*/ | |
module.exports = function (str) { | |
var md = marked(str) | |
return md.replace(/@@(.+?)@@/g, '<blink>$1</blink>') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 9 | |
/*In this adventure, export a module that searches for the string `x=` followed by | |
numbers and returns the number found after the equal sign | |
*/ | |
module.exports = function (str) { | |
var m = /x=(\d+)/.exec(str); | |
return m ? m[1] : null | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 10 | |
/*Just like before, export a module that searches for `x=` followed by a number and return the number found after the equal sign. | |
Unlike before, your solution should only match `x=` when there are no word | |
characters immediately before `x=` or immediately after the number to the right of the equal sign | |
*/ | |
module.exports = function (str) { | |
var m = /\bx=(\d+)\b/.exec(str); | |
return m ? m[1] : null | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 4 | |
/*Write a module that returns true only when the input string starts with a lower-case vowel or a digit | |
*/ | |
module.exports = function (str) { | |
return /^[0-9aeiou]/.test(str); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 1 | |
/*For our first adventure, create a new file that will receive a string as an | |
argument and should return whether the string contains another string, | |
"LITERALLY". | |
*/ | |
module.exports = function (str) { | |
return /LITERALLY/.test(str); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 6 | |
/*Export a function that takes a string argument and returns whether the string | |
ends with a literal `.` (dot) | |
*/ | |
module.exports = function (str) { | |
return /\.$/.test(str); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 5 | |
/*Write a module that returns true only when: | |
the first character is not a digit | |
and the second character is not a capital letter | |
*/ | |
module.exports = function (str) { | |
return /^[^0-9][^A-Z]/.test(str); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 12 | |
/*Export a function that takes a string argument and returns whether the string contains exactly 8 columns of hex codes formatted like: 0xFF with one or more characters of trailing whitespace after each hex code to separate the columns. | |
The hex codes begin with `0x` and have exactly 2 more characters, which may be uppercase `A-F` or lowercase `a-f` or any digit | |
*/ | |
module.exports = function (str) { | |
return /^(0x[a-fA-F\d]{2}\s+){8}$/.test(str) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 7 | |
/*Export a function that takes a filename string as an argument and returns | |
whether the entire filename is a sequence of one or more digits followed by | |
`.jpg` or `.jpeg` | |
*/ | |
module.exports = function (str) { | |
return /^\d+\.jpe?g$/.test(str); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 13 | |
/*Write a module that accepts a string as its argument and returns an array of the quoted strings contained in the input. Strings will be quoted with double | |
quotes. Your program should include the double quotes in its output. | |
For example, given the string: 'cool "beans" "beep boop" whatever "yay"' | |
your program should return: ['"beans"', '"beep boop"', '"yay"'] | |
*/ | |
module.exports = function (str) { | |
return str.match(/"[^"]*"/g) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Exercise 8 | |
/*Strings in javascript have a `.split()` method. `.split()` takes an argument that can be a string or a regex. | |
Write a module that accepts a string as its argument and returns an array that splits the string on commas separated by arbitrary amounts of whitespace | |
*/ | |
module.exports = function (str) { | |
return str.split(/\s*,\s*/); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment