Skip to content

Instantly share code, notes, and snippets.

@Anna-Myzukina
Last active September 4, 2018 05:44
Show Gist options
  • Save Anna-Myzukina/af41f2329f8d626ea21fdb7b677ea1f4 to your computer and use it in GitHub Desktop.
Save Anna-Myzukina/af41f2329f8d626ea21fdb7b677ea1f4 to your computer and use it in GitHub Desktop.
regex-adventure
//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);
}
//Exercise 3
/*This time your regex should match only when the given string ends with
"BANANAS"
*/
module.exports = function (str) {
return /BANANAS$/.test(str);
}
// exercise 2
/*This time your regex should match only when the given string begins with
"LITERALLY"
*/
module.exports = function (str) {
return /^LITERALLY/.test(str);
}
//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
}
//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
}
//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);
}
//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);
}
//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);
}
//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);
}
//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)
}
//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);
}
//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)
}
//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