Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1018 How do I replace all the carriage returns in a string?

irt.org | Knowledge Base | JavaScript | Text | Q1018 [ previous next ]

Q1018 How do I replace all the carriage returns in a string?

For a browser friendly version try:

<SCRIPT LANGUAGE="JavaScript"><!--
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

var testString = 'this\nis\ra\n\rtest';

alert('Before: ' + testString);

testString = replace(replace(testString,'\r','*'),'\n','#');

alert('After: ' + testString);
//--></SCRIPT>

Feedback on 'Q1018 How do I replace all the carriage returns in a string?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.