2

On an xpage we display the activities registrered on an object. the activies are stored in a multi-value Notes field.

a value can be as followed:

2018-12-12 09:36 Jane Doe SysTest/Web/ACME ¤verb_created¤ ¤subj_document¤ in application ¤target_custDB¤

I display the field via a repeat control. In the repeat I have a computed text control with value:

var str = obj;
var regexp = /\¤(.*?)\¤/;
var translations = regexp.exec(str);
if (null != translations){
    for (i = 0; i < translations.length; i++) { 
        if(null != regexp.exec(str)){
            trans = regexp.exec(str)[0];
            //verb first
            if (null != trans){
                str = regexp.replace(str, history[trans]);
            }
        }
    }
}
return str;

history is here a reference to the history.properties file where all the key-value pair translations reside in.

The code works fine for the first 2 values. All additional values remain the original value e.g.¤target_custDB¤

I guess somethings goes wrong with detecting the strings so I wonder if my regexp is correct?

Patrick Kwinten
  • 1,988
  • 2
  • 14
  • 26
  • Try `var regexp = /¤(.*?)¤/g;` and then `return str.replace(regexp, function($0,$1) { return history[$1] ? history[$1] : $0;})`. The regex can also look like `/¤([^¤]*)¤/g` – Wiktor Stribiżew Dec 13 '18 at 14:04
  • just to elaborate on @WiktorStribiżew 's recomendation. He added the global modifier (g) to the regex, which allows a pattern to make multiple matches – doom87er Dec 13 '18 at 14:38

1 Answers1

2

You may use a regex with a global modifier and pass a callback method as the replacement argument to get the appropriate values:

var history1 = {'verb_created': '2018', 'subj_document': 'DOCUMENT', 'target_custDB': 'TARGET'};
var str = "2018-12-12 09:36 Jane Doe SysTest/Web/ACME ¤verb_created¤ ¤subj_document¤ in application ¤target_custDB¤";
var regex = /¤(\w+)¤/g;
str = str.replace(regex, function($0,$1) { return history1[$1] ? history1[$1] : $0;});
console.log(str);

The /¤(\w+)¤/g regex will match multiple occurrences of

  • ¤ - a ¤ char
  • (\w+) - Group 1: one or more ASCII letters, digits or _
  • ¤ - a ¤ char.

The replacement logic is: if there is a key value pair with Group 1 key, this value is returned for replacement, else, the match is pasted back into the result.

To simply extract these substrings, use

var str = "2018-12-13 16:50 Anna User SysTest/Web/ACME ¤verb_created¤ ¤subj_document¤";
var regex = /¤(\w+)¤/g;
var translations = [], m;
while (m = regex.exec(str)) {
  translations.push(m[1]);
}
console.log(translations);
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563