utility function to remove sharepoint list delimiter

May 6, 2010 at 9:01 PM

Hi Marc,

I am using SPServices to query some lists with great success...thank you!

I am finding that I frequently have to add some regex/replace code to get rid of the ";#" delimiters on choice values returned by sharepoint.  any thought to adding a utility function to clean house on these?

Thanks!

Nathan

Coordinator
May 6, 2010 at 9:07 PM

Hi, Nathan.

I could certainly add something like this as a utility. Have you written a function you think is good for me to test? ;+)

M.

May 6, 2010 at 9:52 PM

 

Hi Marc,

I generally add this to return columns that could have a ;# delimiter:

$(this).attr('ows_ChoiceCol').replace(/[\d;# ]*/g, '');

it would be nice to say $().SpServices.Sanitize($(this).attr('ows_...'));

something like this:

function Sanitize(val) {

   return (val != null) ? val.replace(/[\d;# ]*/g, '') : null;

}

Thanks!

Nathan

Coordinator
May 6, 2010 at 10:07 PM
Edited May 6, 2010 at 10:11 PM

Sanitize may be a little strong.  Some might think it was strip mining. ;-)

Seriously, in some cases, we want to keep the delimiters depending on what we are doing.  I have a bunch of little functions like this one at the bottom of SPServices, but I don't expose them publicly.  Initially this was because I only wanted to expose functions that utilized the Web Services, but then I branched out a bit.

In this case, it looks like you are just removing the ;# delimiters. So if you have a set of values like a;#b;#c you'd end up with abc, correct? When is that useful?

It probably would be more useful to take a string to use as a new delimiter instead. Any other options you think might be useful?

function Sanitize(val, delim) {
   return (val != null) ? val.replace(/[\d;# ]*/g, delim) : null;
}

M.

May 6, 2010 at 10:30 PM

Yes, I'm killing the delimiter!  :)

function destroyDelimiter(...)

Seriously, your plan is a bit nicer...how about putting it into an array.

 

function parseChoice(val) {
  return (val != null) val.replace(/[\d;# ]*/g, ",").split(",");
}

So this could be used like so:

var choiceVal = parseChoice($(this).attr('ows_ChoiceCol'));
//
log(choiceVal[0]);
log(choiceVal[1]);
...
Thanks for entertaining the idea!

Nathan

Coordinator
May 6, 2010 at 10:53 PM

Continuous improvement -- I like that.  So now tell me why just doing the split inline wouldn't make sense?

var choiceVals = $(this).attr('ows_ChoiceCol').split(";#");

M.