Regular Expressions II

HackerRank challenge

by trentHarlem

HTML

<h1>
Regular Expressions II
</h1>

CSS

body {
  background-color: dodgerblue;
  color: whitesmoke;
  font: 1.2em system-ui;
}

JavaScript

function TESTregexVar(s) {
  // RegExp object variable matches a string that 
  //	(1) starts with 'Mr.', 'Mrs.', 'Ms.', 'Dr.', or 'Er.', 
  // (2) followed by one or more letters.
let re = new RegExp("^[MDE][rs]s?\\.{1}[a-zA-Z]+$")

  const myRegex = /^(Mr|Mrs|Ms|Dr|Er)\.[A-Za-z]+$/
  //const re = /^(Dr|Mr|Mrs|Ms|Er).[A-z]+$/ 
  //const re = /^(Mr?s|[MDE]r)\.[A-Za-z]+$/;
  //  double !! converts output to boolean
  const output = !!s.match(myRegex)
  const output2 = !!s.match(re)
  const test = re.test(s)

  return [output2, test];
}

function regexVar() {
  //const re = /^(Mr|Mrs|Ms|Dr|Er)\.[A-Za-z]+$/
    const re = /^(Dr|Mr|Mrs|Ms|Er).[A-z]+$/
  return re;
}

 
console.log(TESTregexVar('Mr.X'), 'true')
console.log(TESTregexVar('Dr#Joseph'), 'false')
console.log(TESTregexVar('Mrs.Y'), 'true')
console.log(TESTregexVar('Er .Abc'), 'false')
console.log(TESTregexVar('Mr....X'), 'false')
 

function main() {
  const re = regexVar();
  const s = ['Mr.X','Dr#Joseph','Mrs.Y','Er .Abc','Mr....X']
  console.log(s.map(item => item.match(re)), 'map1')
  console.log(s.map(item => TESTregexVar(item)), 'map2')
}
//main()