[Javascript] RegExp

Ben Curtis Quixote at LaMancha.org
Wed Oct 10 10:37:30 CDT 2001


> ----- 8< -----
> <script language="javascript">
> function replaceText()
> {
> var strString;
> strString = myform.mytextarea.value;
> strString.replace(/\n/,'')
> alert(strString);
> myform.mytextarea.value = strString;
> }
> </script>
> ----- >8 -----
> 
> Any ideas?

Two, actually.

1) you need to capture the changed string value since the replace() method
doesn't change the original string:
 developer.netscape.com/docs/manuals/js/client/jsref/string.htm#1194258

2) you need to replace all occurrences by using the "g" flag, otherwise you
are only catching the first newline.

<script language="javascript">
function replaceText()
  {
    var strString;
    strString = myform.mytextarea.value;
    strNewStr = strString.replace(/\n/g,'')
    alert(strNewStr);
    myform.mytextarea.value = strNewStr;
  }
</script>

Untested, but ought to work.

--
+Ben Curtis
...established 1971.










More information about the Javascript mailing list