Visokio website     Downloads     Video tutorials     KnowledgeBase  
Extracting text using Javascript - Visokio Forums
Extracting text using Javascript
  • Guy_Cuthbert        Guy_Cuthbert March 7, 2012 6:12AM
    Since 2.6, Omniscope has had the ability to include Javascript inside formulae - very useful, but potentially daunting for non-programmers. Steve did a great intro to the topic here, and I thought further examples might be useful... so here is a tutorial on how to extract selected text from a field using regular expressions...

    Firstly, the use case to answer the question "Why would I want to do this?":

    Omniscope users familiar with the formula language may well have come across the SEARCH and SEARCHREGEX functions, which return the position at which a particular character/phrase (in the case of SEARCH) or regular expression pattern (in the case of SEARCHREGEX). These functions are often used to extract elements of text - for example, extracting parts of a postcode; the UK postcode comprises 4 sections:

    Structure of a Postcode

    So, from a full postcode, we might want to extract the postcode area (first 1-2 characters) or the postcode district (all characters before the space). Finding the space, and extracting everything left of the space, is fairly easy using the SEARCH function, so I will focus on the trickier example of extracting the postcode area.

    Using SEARCHREGEX, I can do this in Omniscope by detecting the first alpha character (i.e. A-Z) and the first numeric character (i.e. 0-9) and then extract the characters between these two extremes. To do this we would use:

    DECLARE(
    /* Store position of first alpha (A-Z) and first nuymeric (0-9) */
    firstAZ, SEARCHREGEX("[A-Z]",[Postcode]),
    first09, SEARCHREGEX("[0-9]",[Postcode]),
    IF(
    /* Check that we found both an alpha and a numeric, and that the numeric is after the alpha i.e. a valid postcode format */
    AND(firstAZ > 0,first09 > firstAZ),
    /* Extract the alpha characters, if valid */
    MID([ Postcode], firstAZ, first09-firstAZ),
    /* Return nothing if postcode invalid */
    NULL
    )
    )


    However, regex extraction is simpler and (once we're confident in regex - lots of tutorials online) allows for far more complex extractions. So here is the same function using Javascript:

    SCRIPT(`
    // Declare the pattern we want to extract, and then apply it to our input text
    var re = /([A-Z]*)/;
    var m = re.exec(text1);
    // We should have one (or more) groups of character sets now
    if (m == null) {
    // If not, then return "No"
    'No'
    } else {
    // If we do have at least one character group, return the first (could return many, concatenated, if required)
    m[0]
    }
    `, "text1", [Postcode])


    The structure of this is:
    1. Firstly we have the Javascript code itself, which includes the use of a variable called "text1"
    2. Then, in the last line, we have the declaration of "text1" (this can be called anything, and needs to be the same name here as inside the script itself, and passing the value of our [Postcode] field into this variable, for use in the script

    This only scrapes the surface of this (Javascript regex) approach - there are many ways to use this as the basis for far more complex, and valuable, text extractions... but as Steve's original article demonstrates, there are many, many more uses for Javascript.

    Happy scripting...
    Atheon Analytics Ltd
    w: www.atheonanalytics.com
    e: guy.cuthbert@atheon.co.uk
    t: +44 8444 145501
    m: +44 7973 550528
    s: guycuthbert
This discussion has been closed.
← All Discussions

Welcome!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Apply for Membership