Javascript Replace RegExp Multiline Primer Tutorial

Javascript Replace RegExp Multiline Primer Tutorial

Javascript Replace RegExp Multiline Primer Tutorial

Lately we have had a couple of web application projects using HTML textarea elements extensively. When you involve this particular HTML element type ascii code 10 (line feed) comes into play, and we, as programmers, may need to contend with …


multiline data

… in this scenario. It’s easy enough to use Javascript replace function combined with regular expressions (RegExp) to simulate for a variable x what x.trim() does for a single line of string data (with no ascii code 10 nor 13), as per …


var outstr = x.replace(/^\s+|\s+$/g,'');

… but I’d never used, nor thought to use, the Javascript replace function combined with regular expressions to trim a whole paragraph of string data, as you might find resulting from the use of an HTML textarea element. We’d like to thank this great link, for the heads up, in this regard, used in this Javascript function (suitable for multiline string data)


function myTrim(x) {
return x.replace(/^\s+|\s+$/gm,'');
}

… used in today’s HTML and Javascript javascript_regex_multiline.html‘s proof of concept web application below can show you this in action …

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>