-2

I think this is very easy but I can't figure it out.

var str = "How are you doing today?";
var res = str.split(/\s+/g);

And the result would be: How,are,you,doing,today?. When user add character "|" between two words like this

var str = "How are you |doing today?";

The result shoul be: How,are,you doing,today?

Striped
  • 2,544
  • 3
  • 25
  • 31
Aleksandar
  • 11
  • 5

3 Answers3

0

Try this:

var source = "How are you |doing today?";
var result = source
  .split(/\s+(?!\|)/g)
  .map(function(segment) {
    return segment.replace(/((\s+)\|)/g, '$2');
  });
  
console.log(result);

First the string is split onto whitespace sequences that are not followed by a pipe character (|) using a negative lookahead, then the resulting array is mapped such as that for each segment whitespace sequences followed by a pipe character are replaced with only the whitespace part ($2) of each matching group ($1).


In case that Array.map is not implemented in Extendscript, here's another example using a regular for loop:

var source = "How are you |doing today?";
var rawArray = source.split(/\s+(?!\|)/g);
var result = [];

for (var i = 0; i < rawArray.length; i++) {
  result[i] = rawArray[i].replace(/((\s+)\|)/g, '$2');
}
  
console.log(result);
JJWesterkamp
  • 7,559
  • 1
  • 22
  • 28
  • Hi Jeffrey, arrow function doesn't work in extendscript. Is there any other way to do this. Thanks – Aleksandar Feb 11 '18 at 11:23
  • @A I already replaced ES2015 features with ES5 earlier. Does it still not work? Maybe `Array.map` is not implemented in Extendscript? I have updated my answer with an additional example. – JJWesterkamp Feb 11 '18 at 11:27
  • I've just found that (Array.reduce(), Array.forEach(), Array.map(), etc...). doesn't work in extednscript. – Aleksandar Feb 11 '18 at 11:33
  • Please try the second snippet in my answer, using a `for` loop and intermediate variable as a substitute to `Array.map`. – JJWesterkamp Feb 11 '18 at 11:36
  • Now it's working but it delete "doing". The result is: "How are you today?" – Aleksandar Feb 11 '18 at 11:48
0

You can't do what you describe just split. You could get close using a negative lookahead assertion (/ (?!\|)/), but it leaves the | in the string:

var str = "How are you |doing today?";
var arr = str.split(/ (?!\|)/);
console.log(arr);

...so we have to do more. Probably the simplest thing is to replace the space-plus-bar with a space after the split:

var str = "How are you |doing today?";
var arr = str.split(/ (?!\|)/).map(function(s) {
  return s.replace(/ \|/g, ' ');
});
console.log(arr);

or in ES2015+:

const str = "How are you |doing today?";
const arr = str.split(/ (?!\|)/).map(s => s.replace(/ \|/g, ' '));
console.log(arr);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

First replace all white spaces with a pipe. Then replace by a white space each time you encounter two or more pipes. Then split by pipe :

var str = `How  are you |doing today?
fine | thanx`;
var arr = str.replace(/ {1,}/g,"|").replace(/\|{2,}/g," ").split(/\||\n/);
console.log(arr);
scraaappy
  • 2,830
  • 2
  • 19
  • 29