JSFiddle - React, Tailwind, and code Playground
by asdf
JavaScript
// match Smith or smith, but not blacksmith
var rex = /^[sS]mith/;
console.log(rex.test('blacksmith'));
console.log(rex.test('smith'));
console.log(rex.test('Smith'));
console.log(rex.test('sMith'));
console.log('====================')
// match any of these: Jeffrey, Geoffery, Jeffery, Geoffrey, Jeff, George
rex = /(Jeff((re|er)y)?)|(Geo(rge|(ff(re|er)y)))/;
console.log(rex.test('Jeffrey'));
console.log(rex.test('Geoffery'));
console.log(rex.test('Jeffery'));
console.log(rex.test('Geoffrey'));
console.log(rex.test('George'));
console.log(rex.test('Jeff'));
console.log('====================')
// find dollar amount with optional cents, ex. $4.05 or $76
// s = 'Price: $4.05"
rex = /\$\d+(\.\d{1,2})?/;
console.log(rex.exec('Price: $4.05'));
console.log(rex.exec('Price: $'));
console.log(rex.exec('Price: $4'));
console.log('====================')
// time of the day: 9:05am or 12:30pm note: {min,max}
rex = /((1[0-2])|[1-9])\:[0-5][0-9](am|pm)/;
console.log(rex.exec('15:65pm'));
console.log(rex.exec('9:05am'));
console.log(rex.exec('12:30pm'));
console.log('====================')
// swap first and last name: "Hopper, Grace\nMcCarthy, John\nRitchie, Dennis"
// should produce "Grace Hopper/nJohn McCarthy\nDennis Ritchie"
rex = /(\w+),\s+(\w+)/g;
var str = 'Hopper, Grace\nMcCarthy, John\nRitchie, Dennis';
console.log(str.replace(rex, '$2, $1'));
console.log('====================')
// uppercase FBI and CIA abbreviations in this text: "John was working for fbi, but not cia. Agency (fbi) was investigating eciapa fraud."
// note: ,function
rex = /\b(fbi|cia)\b/ig;
var str = 'Fbi John was working for fbi, but not cia. Agency (fbi) was investigating eciapa fraud.';
console.log(str.replace(rex, function(result){
return result.toUpperCase();
}));
console.log('====================')
// match ip-address, i.e. 12.3.4.5 or 255.255.255.255 or 1.234.5.67
// var exp = '(((1[0-9][0-9])|(2(([0-4][0-9])|(5[0-5]))))|(\\d{1,2}))';
// rex = new RegExp(exp + '\\.' + exp + '\\.' + exp +...