-2

I have quite a similar input to output code scenario as this one: http://plnkr.co/edit/eBjenGqgo3nbnmQ7XxF1?p=preview

I am using AngularJS 1.5.7. Ng directives that my input - ( first ) textarea uses are the same as those shown in Plunkr (ng-list, ng-trim...). My output, unlike the Plunkr example, is displayed inside a ( second ) textarea, but logic is quite similar. $scope.outputValue is an output array which I am converting to string, and implementing 1 RegExp afterwards:

$scope.outputValue  =  $scope.outputValue.toString();
$scope.outputValue  =  $scope.outputValue.replace(/,/g, " | ");

Problem is that the generated string always adds an extra " | " at the end of itself. Often times an output looks like this if the user passes a couple of empty array items inside a first textarea:

var foo = "one | two | three | four | | | | |";

But what I really need to display is this:

var foo = "one | two | three | four";

The only thing that is important is to remove all the "|" that get attached to the end of the string as a replacement of ',' but the output can have "|" values as well, therefore:

var foo = "one| | ||two| | three| | four||";

would also be a valid output.

Similar thing happens with Plunkr example, as it generates empty array items and separated by commas.

Is there any RegExp that might be useful for this problem?

Dharman
  • 30,962
  • 25
  • 85
  • 135
kneles90
  • 75
  • 6
  • please add the outcome of `"one| | ||two| | three| | four||"`. – Nina Scholz Nov 09 '16 at 20:00
  • @NinaScholz that is the outcome, the input would be: ["one|", "||two|", "three|", "four||"] that is an array which would later be converted to string – kneles90 Nov 09 '16 at 20:07
  • so you bring in another aspect, going from an array. please update your question with some more usecases, eiter as array or string and the wanted result. for me, if there is a rule which is hidden in the array, i would like to get better the array with the values. – Nina Scholz Nov 09 '16 at 20:11

3 Answers3

1

You can use regular expression.

 var foo = "one | two | three | four | | | | |";
 foo = foo.replace(/(\s?\|)+$/g, '');
 console.log(foo); //one | two | three | four

Here you can learn more about regular expressions in javascript.

MDN Regular Expressions

MDN String.prototype.replace()

snnsnn
  • 10,486
  • 4
  • 39
  • 44
1

Instead of doing toString() and .replace() on the array, use .join().

   function transform(stringArray) { 
       return stringArray.join(" | ");
   };

  //names = ["morpheus","neo","trinity"] 
  //output = morpheus | neo | trinity

The DEMO on PLNKR


It still outputs vertical bar characters at the end if you don't pass any value in array items.

Add a filter to remove items that are only white space:

  function transform(stringArray) {
       var filteredArray = stringArray.filter((s)=>(s.trim()));
       return filteredArray.join(" | ");
   }

The DEMO on PLNKR.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • It still outputs vertical bar characters at the end if you don't pass any value in array items. Try hitting a couple of whitespaces and returns to see what I mean. Tnx for the effort though – kneles90 Nov 09 '16 at 21:15
  • Added a filter for whitespace items – georgeawg Nov 09 '16 at 21:49
0

Try using slice, as such:

foo = foo.slice(0,-1)
Paul Stoner
  • 1,359
  • 21
  • 44