Javascript User Definable Replace RegExp Multiline Tutorial

Javascript User Definable Replace RegExp Multiline Tutorial

Javascript User Definable Replace RegExp Multiline Tutorial

Almost a year on, we revisit the Javascript Replace RegExp Multiline ExecCommand Tutorial, because, after all, if you are an interpretive substitutional kind of thinker you will know Javascript’s replace is the bee’s knees.

Yesterday’s ‎AppML Acronyms Hierarchy Tutorial‘s coding work reminded us about the many sides to (the oh so easily overlooked, because it is not new) replace

  • how ditching the RegExp ideas for the first argument, it only replaces once … a weakness you say?! … over the years now, we’d disagree … encase it in a while such as …

    var x="The rain\nIn Spain\nFalls Mainly\nOn the plain.";
    while (x.indexOf(String.fromCharCode(10)) != -1) {
    x=x.replace(String.fromCharCode(10), ' ');
    } // ... would result in x equals "The rain In Spain Falls Mainly On the plain."

    … if you want that “substitute all” effectiveness (with a non RegExp first argument) … but, as yesterday reminded us with the AppML “[object Object]” hardcoding we want to change, replace is great that a “replace command” for each “[object Object]” case can be tailored, in the proper order dynamically, in the Javascript code … and though we can’t remember precisely, we have leant on this quirk of replace many times in the past … whereas …
  • how using RegExp ideas for the first argument can perform generic replaces (with “g” usage, rather than “s” (for single) usage) (even across line breaks (or not), as required, as today’s tutorial might be reminding you)

With this in mind, we’ve opened up the underlying web application here, to user entered or modified replace clauses (in two new textboxes) for the user to experiment all the more the wonders of this replace Javascript function brilliance.

Feel free to get in your practice with the changed javascript_regex_multiline.htm live run link.


Previous relevant Javascript Replace RegExp Multiline ExecCommand Tutorial is shown below.

Javascript Replace RegExp Multiline ExecCommand Tutorial

Javascript Replace RegExp Multiline ExecCommand Tutorial

The star HTML element of the recent Javascript Replace RegExp Multiline Primer Tutorial was the …

  • textarea … and a regular reader will know how we here like to associate textarea thinking with …
  • div contenteditable=true

… thinking, representing alternative text editor tools, and in the latter case, quite a bit more. Read more regarding this with our HTML Textarea and Div Talents Primer Tutorial here. One of those “mores” is “cursor selection” possibilities, a whole field of Javascript functionality summarizable via …


document.designMode = "on";
document.execCommand(command, showUI, value);

In fact, though, a whole range of HTML elements other than textarea can respond to “a selection” and then an “execCommand command” be applied to change its look (simulating that “contenteditable=true” global attribute arrangement we favour mostly with HTML div elements), a functionality of some power and use and interest to programmers and web designers, we’re sure.

You might think we are shoving this functionality, a bit esoterically, into this changed javascript_regex_multiline.htm live run link, but we think one “proof of concept” can beget another “proof of concept” into the realms of user desirability for your more adventurous types willing to give ideas a go, on the way to “product development”. Today’s tutorial picture shows a few such “execCommand” changes in action.


Previous relevant Javascript Replace RegExp Multiline Primer Tutorial is shown below.

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.


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


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 *