Replacing U+00A0 with U+0020 as Unicode using Google Apps Script

Gists

This is a sample script for checking and replacing a character of U+00A0 (no-break space) with U+0020 (space) as Unicode using Google Apps Script.

When I’m seeing the questions on Stackoverflow, I sometimes saw the situation that the script doesn’t work while the script is correct. In this case, there is the case that the reason is due to U+00A0 being used as the spaces. When U+00A0 is used as the spaces, Google Apps Script and formulas cannot be correctly run. I thought that when this information is published, it might be useful for a lot of users.

In this post, I would like to introduce how to check and replace U+00A0 with U+0020 using Google Apps Script. Of course, this sample script can be also used with Javascript.

Checking U+00A0 (no-break space)

In this sample, it investigates whether U+00A0 (no-break space) is used in a text using Google Apps Script.

const text = "sample sample"; // or `sample${String.fromCharCode(160)}sample`; This text includes a character U+00A0.
const res = /\u00A0/gi.test(text);
console.log(res);

In this sample, a space between sample is a character of U+00A0. When U+00A0 is included in text, res returns true.

Replacing U+00A0 (no-break space) with U+0020 (space)

In this sample, it investigates whether U+00A0 (no-break space) is used in a text using Google Apps Script.

const text = "sample sample"; // or `sample${String.fromCharCode(160)}sample`; This text includes a character U+00A0.
const res1 = text.replace(/\u00A0/gi, "\u0020");
console.log(res1);

const res2 = /\u00A0/gi.test(res1);
console.log(res2);

In this sample, a space between sample is a character of U+00A0. When this script is run, U+00A0 is replaced with U+0020. Here, you can retrieve the replaced text by res1. And, res2 returns false. By this, it is found that U+00A0 could be replaced with U+0020.

References

 Share!