I have some condisional text that i need to insert into the document based on text extracted With the datamapper. I have created some snippets for the different texts. My problem is that this sometimes have a / character and also sometimes has additional text that is spesific for a individual record (eg. text kr. 1617) so here i need to strip the number?? I have it working as long as it is just plain text but as we know it is not possible to name a snippet With a /.
here is my original script:
var typetxt = loadhtml(‘snippets/typetxt-’ + record.fields.Type + ‘.html’);
Without more examples it’s hard to see what the need is, exactly. You say there are / characters, but your example is kr.1617.
There are many ways you can change your field’s value to fit specific rules, depending on your needs. Here’s a few of those examples:
// Swaps non alphanumerical characters for an underscore. Multiple concurrent characters replaced by a single one. For example: ‘aa bb/*&&cc’ would become ‘aa_bb_cc’
var snippet_type = record.fields.Type;
var clean_type = snippet_type.replace(/[\W]+/g," ");
results.html(clean_type);
//Takes the first part of a string separated by a ‘.’ separator. for example ‘kr.1617’ would becomes just ‘kr’:
var snippet_type = record.fields.Type;
var first_part = snippet_type.split(‘.’)[0];
results.html(first_part);
//Combine the two with ease!
var snippet_type = record.fields.Type;
var clean_type = snippet_type.replace(/[\W]+/g," ");
var first_part = clean_type.split(‘_’)[0]; results.html(first_part);