Regex in script include

JusCuz
Tera Guru

I'm trying to process a regex replace in a script include. The code will work in a background script, but fails in a script include.

regex: function(input) {
gs.info('input= '+input);
var cleaned = input.toString().replace(/[-+()\s]/g, '');
gs.info('A= ');
return cleaned;

 

the first gs.info will run, I never get anything from the second. What am I doing wrong?

1 ACCEPTED SOLUTION

JusCuz
Tera Guru

A co-worker who is a much better developer than I found the issue. Apparently AJAX does not bring over the input as a true javascript string. I did see another post where someone mentioned the same thing, but that post didn't have a good way to correct it. The following line corrects the issue:

var stringInput = new String(input);

The script window complains about using String as a constructor, but it does fix the problem.

View solution in original post

6 REPLIES 6

DScroggins
Kilo Sage

Try the following:

regexCustom: function(input) {
gs.info('input= '+input);
var cleaned = input.toString().replace(/[-+()\s]/g,"");
gs.info('A= ');
return cleaned;

 

The name of your function may be colliding with OOB scripts as well.

JusCuz
Tera Guru

Yea, that is a good point. Just changed it and tried again.....no change unfortunately.

ggg
Giga Guru

replace gs.info('A= ');

with

gs.info('A = ' + cleaned);

 

my br is:

(function executeRule(current, previous /*null when async*/) {
var s = current.short_description;
    gs.log('test1 short desc ' + s);
    //gs.print(regex('a-b'));
    var sReplace = regex(s);
    gs.log('test1 after ' + sReplace);
    current.setValue('short_description', sReplace);

function regex(input){
    gs.log('test1 input= '+input);
    var cleaned = input.toString().replace(/[-+()\s]/g, '');
    gs.log('test1 A= ' + cleaned);
    return cleaned;
}
    //var a = new ArrayUtil();
    //a.push('a');
    //gs.log('mal1 ' + a);

})(current, previous);

 

JusCuz
Tera Guru

Well the problem with that is it has nothing to do with the function of the script. My issue is that that particular gs.info is not being processed, not that it's not containing the value of my variable.