1

I was trying to format a list of dictionary keys for export when I ran into this odd problem.

When trying to replace a decimal point in a string, the ee.String.replace() method seems to treat all characters as decimal points.

//function to remove decimal in second position
var str = ee.String('Sally was a li*ttle lamb. her fleece was white as yo. ')

var regex = '.'


var cut = function(string){
  var newString = ee.String(string);
  //remove decimal
  var decimalTrim = newString.replace(regex, '#');
  return decimalTrim;
};

var testCut = cut(str);
print('testCut', testCut);


var cut2 = function(string){
  var newString = ee.String(string);
  //remove decimal
  var decimalTrim = newString.replace(regex, '#', 'g');
  return decimalTrim;
};

var testCut2 = cut2(str);
print('testCut2', testCut2);

I can achieve the desired results use slice and concatenate methods, but I'm curious to see why my code isn't working. I'm assuming the period is some sort of wildcard, but can't find anything about this in the documentation. Attempting to replace a '*' character will also yield an error message. Anyone knows what's going on here?

1

Standard regular expression syntax uses . to mean "any character". Use \. to refer to a literal period — and since it's quoted as a string you have to double the backslash for that.

var regex = '\\.';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.