Gossamer Forum
Home : General : Internet Technologies :

JavaScript function replaceString()

Quote Reply
JavaScript function replaceString()
I have the following code. It can just replace the first occurrence of a string. I need to replace all the same strings in the text. Could you somebody tell me how to modify the function replaceString() using a loop struction? Thanks a lot in advance for your kind help.

<HTML>
<HEAD>
<TITLE>Replacement method</TITLE>
<script language="JavaScript">
function replaceString(regexpression, replaceString){
document.replaceForm.source.value =
document.replaceForm.source.value.replace
(regexpression, replaceString)
}
</script>
</HEAD>
<BODY>
<form name=replaceForm>
<textarea cols=45 rows=5 name=source>
In teh tree-tier client/server architecture,
teh business logic is separated into a
middle tier: an application server is used
to manage teh business logic, and a database
server handles the data access logic, and
the client manages only the presentation
logic
</textarea>
<br><br>
Replace:<input type=text name=toReplace size=10>
with:<input type=text name=replaceWith size=10>
<input type=button value="Replace"
onClick="replaceString(toReplace.value,replaceWith.value)">
</form>
</BODY>
</HTML>
Quote Reply
Re: [wei03] JavaScript function replaceString() In reply to
I would assume you can just do a loop so that it will replace the string untill it's no longer found...

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [wei03] JavaScript function replaceString() In reply to
Code:
while (document.replaceForm.source.value.indexOf(replaceString) != -1) {