String.replace(/s/g,””) deletes one of the characters when the string contains at least 2 % signs separated by only whitespace

I encountered this weird bug while working on an internal library for my work. We use App Script which is based on Javascript. However, there is a weird quirk with the string.replace() function that is causing problems.

Managers often include spaces when sending commands to our email bot so we have a function in a library which removes whitespace. It is:

function cleanSpaces(string){
  return string.replace(/s/g,"")
}

This function works fine unless the string has at least 2 ampersands separated by at least 1 whitespace character and no other characters.

This is a problem because some of the commands sent to the email bot use special characters, including %s to encode data for the request. Inconsistent behaviors when processing the data could cause problems long term, and I don’t know if this bug affects any other special characters.

For testing, I made a function in apps script and a dummy html page. The apps script test function has the following code (with ‘testString’ replaced with the test string):

function myFunction() {
  let string = 'testString'
  Logger.log(cleanSpaces(testString))
}

And the html document has the following code (with ‘testString’ replaced with the test string):

<html><body>
<p id="test"></p>

<script>
let string = 'testString';
string = string.replace(/s/g,"");
document.getElementById("testString").innerHTML = string;
</script>

</body></html>

When the string is set to something like ‘Hello World’, both functions correctly output HelloWorld

When the string is set to something like ‘ % 4 %4% 4%’, both functions correctly output %4%4%4%

However, when the string is set to something like ‘% %’, the html document correctly outputs %% but the Apps Script incorrectly outputs %.

Could someone please let me know why this happens and what (if anything) can be done to fix it.

5

The problem occurs in the Execution Logs when the string is printed.

Instead of printing the values to the Execution Logs, write them elsewhere. In the example below, write the values for the script properties.

function cleanSpaces(string) {
  return string.replace(/s/g, "")
}

function test() {
  ['% %'].forEach(str => {
    const result = cleanSpaces(str);
    PropertiesService.getScriptProperties().setProperty('RESULT' , result);
  });
}

Instead of using the script properties you might find it more convenient to write the results of your cleanSpaces function to a spreadsheet or a text file in an actual project.

1

Moved from Original post by Micheal verbatim and posted below:


SOLVED By a few community members below. Special thanks to Wicket for the detailed explanation.

It is not a bug with the function but with the built in Logger. The string does actually have the correct number of %s. However, when logging multiple %s in a row, the logger breaks and only shows the odd position %s.

Examples:


function test1(){
  Logger.log('%%')
}
// % (Buggy output)


function test2(){
  let str = '% %';
  str = str.replace(/s/g,"");
  Logger.log(str.length)
}
//2.0  (Expected output)

function test3(){
  let str = '01%%456';
  Logger.log(str);
}  
// 01%456 (Buggy Output)

function test4(){
  let str = '01%%456';
  Logger.log(str[5]);
}  
// 5 (Expected Output)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật