Upland OL User community

JS RegExp Workflow


Does the enhanced javascript support RegExp in workflow, for function like match?

I have a small project where i’m setting a variable from a pdf data location and after I’m trying to split the whole string as I need.

For now I have:

var blocAdresa = Watch.SetVariable(“blocAdresa”, Watch.GetVarible);
Watch.log(blocAdresa, 3)

var codClient = blocAdresa.match(/CATRE: ([\d]+)/, /i)[1];

And I get this error.

[0005] W3602 : Error 0 on line 5, column 1: Microsoft JScript runtime error: ‘blocAdresa’ is null or not an object

How can I access the objects in worfklow ejs?

Maybe someone will point me in the right direction.

Thank you,

I just tried match method with Regex and it works fine for me.

> var blocAdresa = Watch.SetVariable(“blocAdresa”, Watch.GetVarible);
You don’t get any variable there.

I think you have a typo:

var blocAdresa = Watch.SetVariable(“blocAdresa”, Watch.GetVarible);

Should maybe:

var blocAdresa = Watch.GetVariable(“blocAdresa”);

Hi @fsh22, can you let me know what you would like to achieve by the following code:

var blocAdresa = Watch.SetVariable("blocAdresa", Watch.GetVarible);
Watch.log(blocAdresa, 3)

var codClient = blocAdresa.match(/CATRE: ([\d]+)/, /i)[1];

Can you also let me know if this is all the code you have applied to the erroring Run Script task? If not, can you share all the applied code please?

Try it this way:

var re = new RegExp(‘CATRE: ([0-9]+)’, ‘i’);
var test_string = ‘CATRE: 123’;
var match = re.exec(test_string);

if (match !== null) {
Watch.Log(match[1], 4);
} else {
Watch.Log(‘not found’, 4);

JScript apparently only supports basic POSIX Regex, so you need to use “[0-9]” instead of the character class “\d”.

That is incorrect. You can specify \d but if you use the RegExp() constructor with a string parameter, as you did in your example, then the backslash has to be escaped, just like for any JS string. So for intance:

var re = new RegExp("CATRE: (\\d+)", "i");

That’s why I prefer declaring the RegExp directly inline instead (just like Marten did in his reply):

var re = /CATRE: (\d+)/i;

Sorry guys, I was to tired yesterday when I started playing around to build a flow.

My main goal is to split pdf per page and read data from pdf, eventually output everything to a csv file.

For now this is how it looks:


PDF Splitter -Method Split on page (1 page per output)

Set Job Infos - based on the region where is the only place where I can get data to manipulate image

Run Script - purpose: grab the %{blocAdresa} and start playing with regex in order create a csv with lines of data. For nowm my script looks like this:

var fileLocation = get(“workDir”) + “\temp\raport_notificari” + “.csv”
var csvHeader = ‘codClient|denumireClient’;
var curentLoop = Watch.ExpandString("%i");

createFile(fileLocation, csvHeader);

var blocAdresa = “”;
var blocAdresa = get(“blocAdresa”);

//yes it’s a match
var codClient = blocAdresa.match(/Catre:\s+([\w]+)\s±/i)[1];
var denumireClient = blocAdresa.match(/-\s+(.+)\s+ Adresa/i)[1];

//View in Debug Mode
Watch.Log(blocAdresa, 3)

var lineOfText = [codClient, denumireClient].join(’|’);
// write the line of text to the fileOut
appendFile(fileLocation, lineOfText);

//go to newline for new text
var outputFile = set(“fileName”, fileLocation);

/* Helpers */
function appendFile(path, content) {
var fs = new ActiveXObject(‘Scripting.FileSystemObject’)
var file = fs.OpenTextFile(path, 8, true)

function get(name) { return typeof name === ‘number’ ? Watch.getJobInfo(name) : Watch.getVariable(name) }
function set(name, value) { typeof name === ‘number’ ? Watch.setJobInfo(name, value) : Watch.setVariable(name, value) }
function log(msg) { try { Watch.log(toString(msg), 2) } catch(e) { WScript.stdout.WriteLine(toString(msg)) } }
function err(msg) { try { Watch.log(toString(msg), 1) } catch(e) { WScript.stdout.WriteLine(toString(msg)) } }
function exp(string) { return Watch.expandString(string); }
function xml(string) { return Watch.expandString(“xmlget(’/request[1]/values[1]/” + string + “[1]’,Value,KeepCase,No Trim)”); }
function toString(value) { try { return JSON.stringify(value) } catch(e) { return ‘’+value } }

function format() {
var as = arguments
return as[0].replace(/{ *(\w+) *}/g,
function(m, i) { return as[i] })

function sanitizeFilename(filename) {
return filename.replace(//|\|:|?|*|"|||<|>/g, ‘-’)

function writeFile(path, content){
var fs = new ActiveXObject(‘Scripting.FileSystemObject’)
var file = fs.OpenTextFile(path, 8, true)

function createFile(path, content){
var fs = new ActiveXObject(“Scripting.FileSystemObject”)
var file = fs.CreateTextFile(path)


  • helper function to wrap text in quotes for escaping
  • @param {string} text
    function wrappInQuotes(text) {
    return ‘"’ + text + ‘"’

Everything works expected, except every time there is a next iteration in pdf splitter my line of text is not appended, it’s overwritten…

The first line of your script is incorrect. You have to escape the backslashes in the file path:

var fileLocation = get("workDir") + "\\temp\\raport_notificari.csv"

In addition to @Phil’s comment I would also like to mention that I assume that the reason for the following…

Everything works expected, except every time there is a next iteration in pdf splitter my line of text is not appended, it’s overwritten…

…is because you’re creating the same file again or overwriting the same file each time you execute the Run Script task. The reason why I assume this is because your code contains the following lines of code:

createFile(fileLocation, csvHeader);


function createFile(path, content) {
    var fs = new ActiveXObject("Scripting.FileSystemObject");
    var file = fs.CreateTextFile(path);

I think copy paste didn’t work properly.

Backslashes were escaped. If they were not, i was getting and error:


thanks for vigiliance :smiley:

That makes a lot of sense!!

Thank you for pointing me in the right direction.

I’m almost there! How can i get rid of these empty line between each line of text?


Can you let me know please what you get as result (logging) when you add the following line of code…

Watch.Log("\"" + lineOfText + "\"", 3); // Edited

…just after the following line of code?

var lineOfText = [codClient, denumireClient].join("|");

I think you meant

Watch.Log((""" + lineOfText + “”"), 3);


1 Like

Found it!

The Write() method of an instance of the TextStream object is used to write a string to the text file.

The WriteLine() method looks like is adding an extra “\n” after each line. I think usually this behavior occurs at the end of file. Everytime I use Notepad++, i see there is empty line at the end of file.