PDA

View Full Version : Javascript Regular Expression Help



pconceptions
March 20th, 2009, 05:55 PM
Hello!

I've been struggling with this for a while and I need a bit of help.

I have and application where a regexp needs to search for a specific word. However, I also want this to be found and replaced when there is a comma or space (and an additional word) after it.

Example.

If I type "Realtor" it replaces it with "RealtorŪ"
If I type, "Realtor, Manager" it replaces it with "RealtorŪ, Manager"

I have everything except the second one working. My regexp is as follows:


var txtarr = new Array();
var regex = /((Realtor-Associate)$|(Realtors)$|(Realtor)$)+/gi;
function realtorFixer(regex, textIn){
var txtarr = textIn.split(",");
if(!regex.test(textIn)){
return result = textIn;
}else {
var isReal = function(txtarr) {
return String(txtarr).match(regex);
}
var oddArray1=txtarr.filter(isReal)+'\u00AE';
var oddArray2=txtarr.filter(isReal)
return result = textIn.replace(oddArray2, oddArray1);

}

}

Any help would be greatly appreciated.