Uppercase and Lowercase with RegEx

Check if a string’s first letter is lowercase (^ checks beginning of input):

function hasLowerCase(str) {
    return (/^[a-z]/.test(str));
}

Check if a string’s first letter is uppercase:

function hasLowerCase(str) {
    return (/^[A-Z]/.test(str));
}

Check if a string contains lowercase anywhere:

function hasLowerCase(str) {
    return (/[a-z]/.test(str));
}
Advertisements
Uppercase and Lowercase with RegEx

Exercism JS: Word Count

Write a program that given a phrase can count the occurrences of each word in that phrase.

For example for the input "olly olly in come free"

olly: 2
in: 1
come: 1
free: 1

Solution in JS: 

screen-shot-2017-01-04-at-9-40-02-pm

I’ve done this problem before in Ruby and it was a breeze.  However, this version in JS was a bit harder because there are more test cases to account for, which was where regex came in.

Exercism JS: Word Count

Hackerrank Day 28: Regex Patterns & Databases

RegEx (Regulation Expressions)  

This is a means of describing a set of strings using a subset of common characteristics. For example, the common characteristics of aaa, aba, and aca are that they all start and end with the letter a.

Regular expressions add a level of abstraction to strings that makes it easier to search and manipulate text. While they can seem very confusing at first, the syntax for regular expressions is pretty standard across different programming languages.

This is a good guide for common regex used in Ruby.

screen-shot-2017-01-04-at-1-09-17-pm

Could not solve this problem using RegEx in Ruby and could not find anyone else that did either so I’m frustrated, but I don’t feel too bad.  :/

Solution without RegEx:

screen-shot-2017-01-04-at-1-07-58-pm

Talked to Thomas and he suggested removing the .com and just search of emails that have ‘@gmail’ so we can avoid dealing with the period.  Pretty smart!

Solution with RegEx: 

screen-shot-2017-01-04-at-1-25-08-pm

 

 

 

 

 

Hackerrank Day 28: Regex Patterns & Databases

Exercism JS: Bob

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

  • Bob answers ‘Sure.’ if you ask him a question.
  • He answers ‘Whoa, chill out!’ if you yell at him.
  • He says ‘Fine. Be that way!’ if you address him without actually saying anything.
  • He answers ‘Whatever.’ to anything else.

My Solution in JS:

screen-shot-2016-12-28-at-10-39-51-pm

Things to note:

  • Second part of line 11 is a clever way (that I did not come up with myself) to check if a string is alpha characters or not.  (You can also use regex to check if a string fits a certain criteria for characters.)
  • .trim() in Javascript is the same .strip in Ruby.  (They remove whitespace).
Exercism JS: Bob