2

I have this code that works quite well and returns my String: "91308543 91502466 91502466 91503362 91503362 91308543 9851236".

function f () {
var text = ("91308543 91502466 91502466 91503362 91503362 91308543 9851236");
var regex = /(9\d{7})/gm;
var compare = text.match(regex);
var result = compare.join(", ");
return result; }
f ();

My problem is that I want to get rid of the duplicate numbers. I have tried in a JS Console to use this Regex instead: "/(9\d{7})(?!.*\1)/gm;" and it works, but unfortunately in the engine that I use (Esko Automation Engine) is not working at all. I receive this error:

javax.script.ScriptException:TypeError:null has no such function"join" in at line number 9.

Does anyone know a solution or another way to not return the duplicate numbers in the string?

Kayra
  • 83
  • 1
  • 7

1 Answers1

0

This is not regex, but the filter function in js can remove duplicates really well. It's actually a pattern I learned on StackOverflow.

f () {
var text = ("91308543 91308543 91502466 91503362 91503362");
var arr = text.split(' ');
var result = arr.filter( (first, current) = {return arr.indexOf(first) === current});
return result.join(' ');
}
f ();

I'm not familiar with your environment at all but, if join() is your only problem, you could try replacing join() with concat().

function f () {
var text = ("91308543 91502466 91502466 91503362 91503362 91308543 9851236");
var regex = /(9\d{7})/gm;
var compare = text.match(regex);
var result = ''.concat(...compare);
return result; }
f ();

This is using the ...es6 spread operator and concat. The spread operator pretty much takes all the variables in an array and displays them as one. It is the same as ''.concat('91308543', '91502466', '91503362')

Devin
  • 134
  • 9