Click to See Complete Forum and Search --> : string replacement.


nbenton99
12-19-2003, 10:47 AM
Hi, I'm trying to do a simple or not so simple string replacement with a variable. The problem I'm having is that it is only replaceing one instance of the variable rather than every instace within the string.


here's all my code.


var globalAlertMsg="Please correct the following fields:\n";
var l_Msg = globalAlertMsg.length;


function formCheck(){
if (globalAlertMsg.length == l_Msg){
return true;
}else{
alert(globalAlertMsg);
return false;
}
}

function isDate(val,format,formField) {

//document.CurrentForm.elements[formField].select();
var alertMsg = "Please correct the following fields:\n";

var l_Msg = alertMsg.length;

var date=getDateFromFormat(val,format);
if (date==0) {
//document.CurrentForm.elements[formField].select();
//eval("document.CurrentForm." + formField + ".select()")
alert("this is " + formField +" has a wrong date");
globalAlertMsg += " - " + formField + "\n";
return globalAlertMsg;
//eval("document.CurrentForm." + formField + ".focus()")

return false;

}
else {
alert("this is a right date");
//document.CurrentForm.elements[formField].blur();
alert(globalAlertMsg);

editout = " - " + formField + "\n";
str = globalAlertMsg;

while(str.search(editout) != -1){
str = str.replace(editout, '');
globalAlertMsg = str;
return globalAlertMsg;
}


alert (str);
alert(globalAlertMsg);
return true;
}
}


Here's the section I'm having trouble with.

alert("this is a right date");
//document.CurrentForm.elements[formField].blur();
alert(globalAlertMsg);

editout = " - " + formField + "\n";
str = globalAlertMsg;

while(str.search(editout) != -1){
str = str.replace(editout, '');
globalAlertMsg = str;
return globalAlertMsg;
}



Any help would be much appreciated.

TIA

Nikki

olerag
12-19-2003, 12:06 PM
1. Don't you need initialization for str and editout, such as
var str, editout;

2. If its the global your trying to add contents to try..
globalAlertMsg += str instead of globalAlertMsg = str;

Hopefully this will help...

nbenton99
12-19-2003, 12:17 PM
Hi thanks for your help

I tried adding the var (I didn't think it was mandatory) but I added it now and it still doesn't work. I don't what to add the str to the globalAlertMsg I want to reset the globalAlertMsg to the value of str. the problem I'm having is that I there maybe multiple instances of the value be pass through the editout variable and I need to remove not just one but all instances. here's the code I am taking it from which works fine.

Example 1. In the following example, the regular expression includes the global and ignore case flags which permits replace to replace each occurrence of 'apples' in the string with 'oranges.'

<SCRIPT>
re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr=str.replace(re, "oranges");
document.write(newstr)
</SCRIPT>

This prints "oranges are round, and oranges are juicy."



I think the problem I'm having is that I'm passing a variable withing a variable

var editout = " - " + formField + "\n";

thats why I then tried doing it through a while statement but that doesn't see to work either. Although I think both case should work I'm wondering if there is just something small wrong with my sintax.

Any more suggestions.

TIA

NIkki

olerag
12-19-2003, 01:30 PM
Sorry - wasn't with you at first. I believe your problem is that
you can have more than one occurance of a substring portion
in a string and you'd like all occurances replaced with a new
substring value. Such as your example of replacing "apples"
with "oranges", regardless of case.

In the demo below, the value in "Field1" is always "Apples".
Consequently, the global value (containing both "Apples" and
"apples") will change to whatever you enter in Field2.

Note that the output that is displayed will change both values
to the case you entered in field2.


<html>
<head>
<script type="text/javascript">
var gvString = "Apples are round and apples are juicy.";

function replaceDemo(form) {
var re = new RegExp(form.field1.value.toLowerCase(),"g");
alert(gvString.toLowerCase().replace(re, form.field2.value));
}
</script>
</head>
<body>
<center>
<b>String Replacement Demo</b>
<form>
<input type="text" name="field1" size="15" length="15" value="Apples" disabled>
<input type="text" name="field2" size="15" length="15">
<p>
<input type="button" name="replaceBtn" value="Show Replace Results" onClick="replaceDemo(this.form)">
</form>
<hr>
</center>
</body>
</html>