This little gem is great for replacing a character(s) in a string. In this instance I’m stripping the dash from a phone number by searching for “-” and replacing it with “”.

// The function that you'll call to replace a character
function replace (origStr, searchStr, replaceStr) {
  var tempStr = "";
  var startIndex = 0;
  if (searchStr == "") {
    return origStr;
  }
  if (origStr.indexOf(searchStr) != -1) {
    while ((searchIndex = origStr.indexOf(searchStr, startIndex)) != -1) {
      tempStr += origStr.substring(startIndex, searchIndex);
      tempStr += replaceStr;
      startIndex = searchIndex + searchStr.length;
    }
    return tempStr + origStr.substring(startIndex);
  } else {
    return origStr;
    }
  }

// Now I'm calling the replace function with my source string (phoneNum), what I want to replace(-), and what to replace it with (empty), then saving the result in newPhoneNum
var newPhoneNum = replace(phoneNum, "-", "");