PDA

View Full Version : line break determination


epopov
01-02-2004, 03:05 AM
Hi,
I want to replace line break in the string from inputtext to '<br />' string. I.e. when user presses enter on his keyboard I want to include '<br/>' into the string.
Is it possible? How to perform this?

Thanks.

antun
01-05-2004, 01:55 PM
In theory, the ECMA-specified String.replace() function would help you here, but unfortunately it's not present in Laszlo (as with most browser JavaScript). So instead I did a quick Google search for a JavaScript Search & Replace function and came up with this:

http://www.petenelson.com/aspwatch/ASPWatch%20%20Javascript%20Search%20and%20Replace. htm

... which I copied and pasted into a LZX file. I searched for \r, and replaced it with <br/>:


<canvas debug="true">

<script><![CDATA[
function SearchAndReplace(Content, SearchFor, ReplaceWith) {

var tmpContent = Content;
var tmpBefore = new String();
var tmpAfter = new String();
var tmpOutput = new String();
var intBefore = 0;
var intAfter = 0;

if (SearchFor.length == 0)
return;


while (tmpContent.toUpperCase().indexOf(SearchFor.toUppe rCase()) > -1) {

// Get all content before the match
intBefore = tmpContent.toUpperCase().indexOf(SearchFor.toUpper Case());
tmpBefore = tmpContent.substring(0, intBefore);
tmpOutput = tmpOutput + tmpBefore;

// Get the string to replace
tmpOutput = tmpOutput + ReplaceWith;


// Get the rest of the content after the match until
// the next match or the end of the content
intAfter = tmpContent.length - SearchFor.length + 1;
tmpContent = tmpContent.substring(intBefore + SearchFor.length);

}

return tmpOutput + tmpContent;

}

]]>
</script>

<simplelayout axis="y" spacing="10" />

<inputtext width="300" height="175" multiline="true" name="foo" />

<button>Get text
<method event="onclick">
var origText = foo.getText();
Debug.write( "original: " + origText );

var newText = SearchAndReplace( origText, "\r", "&lt;br/&gt;" );
Debug.write( "New: " + newText );
</method>
</button>

</canvas>


-Antun