Home > database >  How do I replace every second comma in a string with a space with Javascript?
How do I replace every second comma in a string with a space with Javascript?

Time:09-16

I'm trying to do str.replace(/([0-9] ),\s([0-9] ),\s/g, '$1, $2 ') but this is not working.

For instance, when applied to this input:

35.15560188441565,31.738399966331848 , 35.2517322555094,31.705440981956848 , 35.28057136683753,31.742519839378723 , 35.28743782191565,31.798824771019348 , 35.27507820277503,31.842770083519348 , 35.20504036097815,31.872982485863098 , 35.16521492152503,31.841396792503723 , 35.14049568324378,31.807064517113098 , 35.15560188441565,31.738399966331848

I get the following output:

35.15560188441565,31.738399966331848 , 35.2517322555094,31.705440981956848 , 35.28057136683753,31.742519839378723 , 35.28743782191565,31.798824771019348 , 35.27507820277503,31.842770083519348 , 35.20504036097815,31.872982485863098 , 35.16521492152503,31.841396792503723 , 35.14049568324378,31.807064517113098 , 35.15560188441565,31.738399966331848

CodePudding user response:

While there's nothing wrong with using a regular expression to transform your input string (and I don't discount that you might have some use for the full string), it seems more useful to parse each pair of values representing floating point numbers.

Another approach is to iterate over the string, keeping track of each comma that is seen and collecting the current substring along the way — then, on every other comma, parse the existing substring to get a pair of the existing values. This will produce a more flexible data structure that you can further transform to use in other ways.

Below is an example of a generator function which takes an input string like the one in your question (and an optional transform function) and yields a transformed result for each encountered pair.

I've included an example of the default output and an example of providing a custom transform function to produce lat-lon coordinates objects:

function* parsePairs (inputStr, transformer = (pair) => pair) {
  let splitOnNextComma = false;
  let current = '';

  const trim = (str) => str.trim();
  const getTrimmedPair = () => transformer(current.split(',').map(trim));

  for (const str of inputStr) {
    if (str !== ',') {
      current  = str;
      continue;
    }

    if (!splitOnNextComma) {
      splitOnNextComma = true;
      current  = str;
      continue;
    }

    splitOnNextComma = false;
    yield getTrimmedPair();
    current = '';
  }

  if (current.includes(',')) yield getTrimmedPair();
}

const input = ' 35.15560188441565,31.738399966331848 , 35.2517322555094,31.705440981956848 , 35.28057136683753,31.742519839378723 , 35.28743782191565,31.798824771019348 , 35.27507820277503,31.842770083519348 , 35.20504036097815,31.872982485863098 , 35.16521492152503,31.841396792503723 , 35.14049568324378,31.807064517113098 , 35.15560188441565,31.738399966331848 ';

console.log('default result:');
for (const pair of parsePairs(input)) console.log(pair);

const asCoordsObj = ([first, second]) => ({
  lat: Number(first),
  lon: Number(second),
});

console.log('as coordinates objects:');
const coordsObjArray = [...parsePairs(input, asCoordsObj)];
console.log(coordsObjArray);

CodePudding user response:

If you want to keep the same amount of spaces you can use 2 capture groups, and then replace the second comma with a space:

(-?\b\d (?:\.\d )?\s*,\s*-?\b\d (?:\.\d )?\s*),(\s*)

In the replacement use $1 $2

See a regex demo.

const regex = /(-?\b\d (?:\.\d )?\s*,\s*-?\b\d (?:\.\d )?\s*),(\s*)/gm;

const str = ` 35.15560188441565,31.738399966331848 , 35.2517322555094,31.705440981956848 , 35.28057136683753,31.742519839378723 , 35.28743782191565,31.798824771019348 , 35.27507820277503,31.842770083519348 , 35.20504036097815,31.872982485863098 , 35.16521492152503,31.841396792503723 , 35.14049568324378,31.807064517113098 , 35.15560188441565,31.738399966331848 `;

console.log(str.replace(regex, `$1 $2`));

  • Related