Regular Expressions with JavaScript

From Director Online Wiki
Jump to: navigation, search

Regular Expressions in Director (from Director MX 2004) via JavaScript.

// JavaScript, e.g. in a movie script
function RegEx (myString) {
  var Expression = /(\w.+)\s(\w.+)/;
  Expression.exec(myString);
  trace(RegExp.$2 + ", " + RegExp.$1);
}
-- Lingo, e.g. in the message window
myString = "Shantanu Narayen"
RegEx(myString)

Search & replace of strings:

1. Variation:

// JavaScript, e.g. in a movie script
function replaceString(myStr, mySearchStr, myReplaceStr) {
  var re = new RegExp (mySearchStr, "g");
  return myStr.replace(re, myReplaceStr);
}
-- Lingo, e.g. in the message window
put replaceString("Ok, who knows JavaScript", "who", "Director")

2. Variation:

// JavaScript, e.g. in a movie script
function replaceString(myStr, mySearchStr, myReplaceStr) {
   return myStr.split(mySearchStr).join(myReplaceStr);
}
-- Lingo, e.g. in the message window
put replaceString("Ok, who knows JavaScript", "who", "Director")

Trim word of all characters other than legitimate ones:

function trimWord(str) {
  str = str.replace(/[^a-z0-9'’\-]/gi, "");
  str = str.replace(/’/g, "'");
  str = str.replace(/^\-+|\-+$/g, "");
  str = str.replace(/^'+|'+$/g, "");
  str = str.replace(/^\-+|\-+$/g, "");
 
  return str;
}

Remove extra spaces:

function removeExtraSpaces(str) {
  str = str.replace(/\s\s+/g, " ");
  return str.replace(/^\s+|\s+$/g, ""); 
}

Trim spaces - functions like VB:

function Ltrim(str) {
  return trim(str, "left");
}
 
function Rtrim(str) {
  return trim(str, "right");
}
 
function trim(str, side) { 
  if (side=="left") return str.replace(/^\s+/g, "");
  if (side=="right") return str.replace(/\s+$/g, "");
  return str.replace(/^\s+|\s+$/g, ""); 
}

Test for number:

function testForNumber(str) {
 return str.length != (str.replace(/[0-9]/g, "")).length;
}

Replace single quotes (good for SQL strings):

function replaceSingleQuotes(str) {
  return str.replace(/\'/g, "\'\'");
}

Convert string to lower case:

function LCase(str) {
  return str.toLowerCase();
}

Convert string to upper case:

function UCase(str) {
  return str.toUpperCase();
}

Convert string to title case:

//convertToTitleCase() relies on the cnvrt function.
function convertToTitleCase(str) {
   return str.toLowerCase().replace(/\b[a-z]/g, cnvrt);
}
 
function cnvrt() {
   return arguments[0].toUpperCase();
}

Trim punctuation from string:

function trimPunctuation(str) {
   str = str.replace(/^[;,.():<>\[\]]+/g, "");
   return str.replace(/[:;,.()<>\[\]]+$/g, "");
}

Replace return character with carriage return and line feed:

function replaceReturn(str) {
   return str.replace(/\r/g, "\n\r");
}

Test if an email is valid:

function isValidEmail(str){
regX = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
return regX.test(str);
}

Strong password enforcement:

// Strength levels are from 0 to 2
//This policy setting determines whether passwords must meet a series of guidelines that are considered important for a strong password.
//If this policy setting is enabled, passwords must meet the following requirements:  
//The password is at least six characters long.  
//The password contains characters from three of the following four categories: 
//
//Uppercase characters (A, B, C, ...)
//Lowercase characters (a, b, c, ...)  
//Numerals (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)   
//Non-alphanumeric and Unicode characters (( ) ` ~ ! @ # $ % ^ & * - + = | \ { } [ ] : ; " ' < > , . ? /)
function isStrongPassword(str, lvl, min, max){
if (min == undefined || min < 6) min = 6;
if (max == undefined || max > 30) max = 30;
switch (lvl)
{
case (lvl = 0):
  regX = new RegExp("^(?!\\s)(?!.*?\\s).{" + min + "," + max + "}$");
  //regX = /^(?!\s)(?!.*?\s).{min, max}$/;
break;
case (lvl = 1):
  regX = new RegExp("^(?!\\s).*?(?=.*?[A-Z])(?=.*?[a-z])(?!.*?\\s).{" + min + "," + max + "}$");
  //regX = /^(?!\s).*?(?=.*?[A-Z])(?=.*?[a-z])(?!.*?\s).{6,30}$/;
break;
case (lvl = 2):
  regX = new RegExp("^(?!\\s).*?(?=.*?[A-Z])(?=.*?\\d)(?=.*?[a-z])(?!.*?\\s).{" + min + "," + max + "}$");
break;
default:
  regX = new RegExp("^(?!\\s).*?(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\\d)(?=.*?[()`~!@#$%\\^&*-+=|\\{}\\[\\]:;\"'<>,.?])(?!.*?\\s).{" + min + "," + max + "}$");
break;
}
 
return regX.test(str);
}

Obfuscate email address:

function obfuscateEmail(str, _at, _dot) {
  if (_at==undefined) _at = " at ";
  if (_dot==undefined) _dot = " dot ";
  return str.replace(/@/, _at).replace(/\./g, _dot);
}

Get file extension from a file name:

function getExtension(str) {
  strArr = str.split("\\");
  extArr = strArr[strArr.length - 1].split(".");
  if (extArr.length <=1) return undefined;
  return extArr[extArr.length - 1].toLowerCase();
}

Check Typo: