Saturday, January 13, 2007

Javascript string replace

Didn't know Javascript had regular expression support.

    var str = "abcabc";
str.replace("a", "#");
document.write( str );
will print "abcabc".
    var str = "abcabc";
document.write( str.replace("a", "#") );
will print "#bcabc".
    var str = "abcabc";
document.write( str.replace(/a/g, "#") );
will print "#bc#bc".
    var str = "ABCABC";
document.write( str.replace(/a/i, "#") );
will print "#BCABC".
    var str = "ABCABC";
document.write( str.replace(/a/gi, "#") );
will print "#BC#BC".

No comments:

Post a Comment