0

I want to have a 'newline'-function to pass a string to it and print it on pdf. my function so far is

var array = new Array();
function newLineFunction_PDF(text) {
  var arr = text.replace(/.{70}\S*\s+/g, "$&@").split(/\s+@/);
  return arr;
}

array = newLineFunction_PDF('some Text');

for( var i in array) {
  print(array[i]);
}

What it does is cut the text in to pieces of length-70 incl. the last word, push it into the array and print it afterwards with new lines. Now i want to pass a number to the function, like 100, so i can decide the max-length of the text per line.

So far I tried:

function newLineFunction_PDF(text, num) {
  var re = new RegExp(/.{num}\S*\s+/g);
  var arr = text.replace(re, "$&@").split(/\s+@/);
  return arr;
}

but I dont know how and where to add escapes into the new RegExp.

moody
  • 404
  • 7
  • 19

1 Answers1

1

The parameter of Regexp is a string:

 var re = new RegExp('.{' + num + '}\S*\s+', 'g');
Toto
  • 89,455
  • 62
  • 89
  • 125