Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
14.6 KB
Raw Blame
[user]
I need a regex to strip trailing whitespace from log lines. It should remove spaces and tabs at the end of each line, but leave indentation and spaces inside the line unchanged.

[assistant]
Use this pattern in multiline mode:

```regex
[ \t]+$
```

Replace each match with an empty string. The character class matches one or more spaces or tabs, and `$` anchors the match at the end of each line when multiline mode is enabled.

For example, in many editors or regex libraries:

```text
pattern:    [ \t]+$
replacement:
option:     multiline
```

If the input can use either Unix or Windows line endings, this still normally works because the match stops before the line break. Some engines treat `$` specially around `\r\n`; if a carriage return remains, use `\h+$` where horizontal whitespace is supported, or use `[ \t]+\r?$` and replace with nothing.

[user]
Can you show a concrete test string with visible markers? I want to verify that tabs are removed too, while leading indentation stays.

[assistant]
Here is a test string where `·` represents a space, `→` represents a tab, and `¶` marks the line ending:

```text
··INFO··started→¶
→→DEBUG··value=42··→¶
WARN··message with internal··spaces··¶
··ERROR··finished¶
```

The actual input should contain ordinary spaces, tabs, and newlines, not the visible marker characters. Written as escaped text, the same input is:

```text
"  INFO  started\t\n\t\tDEBUG  value=42  \t\nWARN  message with internal  spaces  \n  ERROR  finished\n"
```

Applying `[ \t]+$` with multiline mode produces:

```text
··INFO··started¶
→→DEBUG··value=42¶
WARN··message with internal··spaces¶
··ERROR··finished¶
```

The two leading tabs on the second line and the two leading spaces on the first and fourth lines remain.

[user]
What changes if I use JavaScript? I’m doing this in a small Node script rather than an editor.

[assistant]
In JavaScript, use the `g` and `m` flags:

```js
const trailingWhitespace = /[ \t]+$/gm;

const input =
  "  INFO  started\t\n" +
  "\t\tDEBUG  value=42  \t\n" +
  "WARN  message with internal  spaces  \n" +
  "  ERROR  finished\n";

const output = input.replace(trailingWhitespace, "");

console.log(JSON.stringify(output));
```

Expected output:

```text
"  INFO  started\n\t\tDEBUG  value=42\nWARN  message with internal  spaces\n  ERROR  finished\n"
```

The `m` flag makes `^` and `$` work per line, and `g` replaces every match rather than only the first one. The pattern deliberately lists only ordinary spaces and tabs, so it does not remove other whitespace characters.

[user]
Could I use `\s+$` instead? It looks shorter.

[assistant]
You can, but it is usually broader than the requirement. In JavaScript, `\s` matches spaces, tabs, line terminators, and several other whitespace characters. With multiline matching, that can make behavior less obvious around line endings, especially when the input uses `\r\n`.

For only spaces and tabs, keep:

```js
/[ \t]+$/gm
```

If you explicitly want all trailing whitespace characters, including unusual Unicode whitespace, you could use:

```js
/\s+$/gm
```

Test the difference with this input:

```js
const input = "INFO ok  \nWARN next\t\nDONE end\u00a0\n";
```

The last line ends with a non-breaking space. `[ \t]+` leaves it alone; `\s+` removes it. Neither pattern should remove indentation at the start of a line, because both patterns are anchored at the end.

[user]
I also have CRLF files. Can you give me a version that is unambiguous about not consuming the line ending?

[assistant]
Yes. Match only spaces and tabs, then require the position immediately before either a carriage-return/line-feed pair or the end of the string:

```js
/[ \t]+(?=\r?$)/gm
```

The lookahead checks for `\r?` followed by the multiline end position without consuming anything. For a CRLF line, the match ends before `\r`, so the line ending remains `\r\n`. For the final line without a line ending, it can match before the end of the string.

Example:

```js
const input = "INFO one  \r\n\tWARN two\t\r\n  DONE three  ";

const output = input.replace(/[ \t]+(?=\r?$)/gm, "");

console.log(JSON.stringify(output));
```

Output:

```text
"INFO one\r\n\tWARN two\r\n  DONE three"
```

This version still preserves leading whitespace and all internal whitespace.

[user]
Does the `$` in that lookahead behave differently with the `m` flag when there is a CRLF pair?

[assistant]
Yes, this is one of the small JavaScript regex details worth testing. With `/m`, JavaScript allows `$` to match before a line terminator, but for a CRLF sequence the position is effectively handled at the end of the line-ending sequence rather than between `\r` and `\n` for the purpose of this pattern. The explicit `\r?` makes the intended position clear.

A simple inspection script is:

```js
const input = "A  \r\nB\t\r\nC  ";
const re = /[ \t]+(?=\r?$)/gm;

for (const match of input.matchAll(re)) {
  console.log({
    text: JSON.stringify(match[0]),
    index: match.index
  });
}
```

Typical output is:

```text
{ text: '"  "', index: 1 }
{ text: '"\\t"', index: 7 }
{ text: '"  "', index: 11 }
```

The replacement does not include either `\r` or `\n`. If your runtime or test setup makes line-ending behavior confusing, normalizing line endings first is another straightforward option.

[user]
Would normalizing line endings first be safer for a script that processes mixed input?

[assistant]
It can make the logic simpler, especially if the input may contain CRLF, LF, and old-style CR line endings. Normalize first, then apply the simple pattern:

```js
const normalized = input.replace(/\r\n?/g, "\n");
const cleaned = normalized.replace(/[ \t]+$/gm, "");
```

This converts CRLF and lone CR to LF. It does change the line-ending representation, so use it only if that is acceptable. If you must preserve the original line endings, do not normalize; use:

```js
const cleaned = input.replace(/[ \t]+(?=\r?$)/gm, "");
```

A small mixed-input example:

```js
const input = "A  \r\nB\t\nC  \rD\r\n";
const normalized = input.replace(/\r\n?/g, "\n");
const cleaned = normalized.replace(/[ \t]+$/gm, "");

console.log(JSON.stringify(cleaned));
```

Output:

```text
"A\nB\nC\nD\n"
```

The trailing whitespace is removed, and every line ending is now LF.

[user]
I want to preserve line endings. Can you show a test harness that checks both the cleaned text and the exact endings?

[assistant]
Use a small assertion helper and keep the CRLF and LF cases explicit:

```js
function stripTrailingSpacesAndTabs(text) {
  return text.replace(/[ \t]+(?=\r?$)/gm, "");
}

const input =
  "INFO one  \r\n" +
  "\tWARN two\t\r\n" +
  "NOTE internal  spaces  here\n" +
  "DONE three  ";

const expected =
  "INFO one\r\n" +
  "\tWARN two\r\n" +
  "NOTE internal  spaces  here\n" +
  "DONE three";

const actual = stripTrailingSpacesAndTabs(input);

console.log("actual:  ", JSON.stringify(actual));
console.log("expected:", JSON.stringify(expected));
console.log("same:", actual === expected);

if (actual !== expected) {
  throw new Error("Output did not match expected text");
}

const endings = [...actual.matchAll(/\r\n|\n|\r/g)].map(match => JSON.stringify(match[0]));
console.log("line endings:", endings);
```

Output:

```text
actual:   "INFO one\r\n\tWARN two\r\nNOTE internal  spaces  here\nDONE three"
expected: "INFO one\r\n\tWARN two\r\nNOTE internal  spaces  here\nDONE three"
same: true
line endings: [ '"\\r\\n"', '"\\r\\n"', '"\\n"' ]
```

The final line has no line ending, so it does not appear in the endings list.

[user]
Can you add cases for blank lines, whitespace-only lines, and a final line that ends with CRLF?

[assistant]
Here is an expanded harness:

```js
function stripTrailingSpacesAndTabs(text) {
  return text.replace(/[ \t]+(?=\r?$)/gm, "");
}

const cases = [
  {
    name: "blank line",
    input: "A  \r\n\r\nB\t\r\n",
    expected: "A\r\n\r\nB\r\n"
  },
  {
    name: "spaces and tabs only",
    input: "A\n  \t  \nB\n",
    expected: "A\n\nB\n"
  },
  {
    name: "final line ends with CRLF",
    input: "A  \r\nB\t\r\n",
    expected: "A\r\nB\r\n"
  },
  {
    name: "indentation and internal spaces",
    input: "\t  A  B  \r\n",
    expected: "\t  A  B\r\n"
  },
  {
    name: "no trailing whitespace",
    input: "A\nB\n",
    expected: "A\nB\n"
  }
];

for (const test of cases) {
  const actual = stripTrailingSpacesAndTabs(test.input);
  const passed = actual === test.expected;

  console.log(`${passed ? "PASS" : "FAIL"}: ${test.name}`);
  if (!passed) {
    console.log(" input:  ", JSON.stringify(test.input));
    console.log(" actual:  ", JSON.stringify(actual));
    console.log(" expected:", JSON.stringify(test.expected));
  }
}
```

Expected output:

```text
PASS: blank line
PASS: spaces and tabs only
PASS: final line ends with CRLF
PASS: indentation and internal spaces
PASS: no trailing whitespace
```

On a whitespace-only line, the entire line content is trailing spaces and tabs, so it becomes an empty line while its line ending remains.

[user]
What about a line ending in a form-feed or a non-breaking space? I need to know exactly what the chosen pattern leaves behind.

[assistant]
With `/[ \t]+(?=\r?$)/gm`, only ordinary U+0020 spaces and U+0009 tabs are removed. A form-feed, U+000C, and a non-breaking space, U+00A0, are not matched and remain.

```js
function stripTrailingSpacesAndTabs(text) {
  return text.replace(/[ \t]+(?=\r?$)/gm, "");
}

const input =
  "FORM feed\f\r\n" +
  "NBSP here\u00a0\r\n" +
  "ordinary  \t\r\n";

const output = stripTrailingSpacesAndTabs(input);

console.log(JSON.stringify(input));
console.log(JSON.stringify(output));
console.log([...output].map(ch => ({
  char: JSON.stringify(ch),
  codePoint: `U+${ch.codePointAt(0).toString(16).toUpperCase().padStart(4, "0")}`
})));
```

The output string is:

```text
"FORM feed\f\r\nNBSP here \r\nordinary\r\n"
```

The form-feed and non-breaking space remain. The ordinary spaces and tab on the last line are removed. This narrow behavior is useful when “trailing whitespace” specifically means spaces and tabs, rather than every Unicode whitespace character.

[user]
If I decide that all horizontal whitespace should be removed, what pattern would you use in JavaScript?

[assistant]
For JavaScript, you can use a character class that covers common horizontal whitespace explicitly:

```js
/[ \t\f\v\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000]+(?=\r?$)/gm
```

This includes:

```text
U+0020  ordinary space
U+0009  tab
U+000C  form feed
U+000B  vertical tab
U+00A0  no-break space
U+1680  ogham space mark
U+2000–U+200A  spaces in the U+2000 block
U+202F  narrow no-break space
U+205F  medium mathematical space
U+3000  ideographic space
```

It still excludes line terminators, so the lookahead can preserve CRLF and LF. A readable version is often easier to maintain:

```js
const horizontalWhitespace =
  /[ \t\f\v\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000]+(?=\r?$)/gm;
```

If you do not need a carefully limited definition, `\s` is shorter, but it includes vertical whitespace and line terminators, so it does not express “horizontal only” as precisely.

[user]
Could Unicode property escapes make that less cumbersome?

[assistant]
Not completely. JavaScript supports properties such as `\p{White_Space}` with the `u` flag, but Unicode’s `White_Space` property includes line terminators, so it is not horizontal-only by itself.

For example:

```js
const allUnicodeWhitespace = /\p{White_Space}+(?=\r?$)/gmu;
```

That is broader than horizontal whitespace and can interact with line boundaries. You can subtract known line terminators with a negative lookahead, but the result is less readable:

```js
const nonLinebreakWhitespace =
  /(?:\p{White_Space}|\p{Line_Break=Glue})+(?=\r?$)/gmu;
```

That example is not a general “horizontal whitespace” definition, and JavaScript property support varies by runtime and property. For predictable Node behavior, the explicit class is clearer:

```js
const horizontalWhitespace =
  /[ \t\f\v\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000]+(?=\r?$)/gmu;
```

Use the narrow `[ \t]` form unless the broader set is actually needed.

[user]
Let’s return to spaces and tabs. Are there any pitfalls with a trailing tab immediately before the CR in CRLF?

[assistant]
The narrow pattern handles that case directly:

```js
const re = /[ \t]+(?=\r?$)/gm;
const input = "INFO value\t\r\nNEXT value  \t\r\nEND";

console.log(JSON.stringify(input.replace(re, "")));
```

Output:

```text
"INFO value\r\nNEXT value\r\nEND"
```

The tab is matched, while `\r\n` remains untouched. The important part is that `[ \t]+` cannot consume `\r`, and the lookahead verifies that the next characters are either `\r\n` in a multiline position or the end of the string.

A few related cases:

```js
[
  "A\t\r\n",
  "A \t \r\n",
  "\t\r\n",
  "A\t",
  "A\t\n"
].forEach(value => {
  console.log(JSON.stringify(value), "=>", JSON.stringify(value.replace(re, "")));
});
```

Expected results:

```text
"A\t\r\n" => "A\r\n"
"A \t \r\n" => "A\r\n"
"\t\r\n" => "\r\n"
"A\t" => "A"
"A\t\n" => "A\n"
```

Leading tabs are not affected unless they are also the entire content of a line, in which case removing them leaves a blank line.

[user]
What if the log is a single line with no final newline? Does the same regex remove its trailing spaces?

[assistant]
Yes. With the `m` flag, `$` can match at the end of the input even when there is no final newline. The lookahead version therefore handles both terminated and unterminated final lines:

```js
const re = /[ \t]+(?=\r?$)/gm;

const samples = [
  "single line  ",
  "single line\t",
  "single line  \n",
  "single line  \r\n"
];

for (const sample of samples) {
  console.log(
    JSON.stringify(sample),
    "=>",
    JSON.stringify(sample.replace(re, ""))
  );
}
```

Output:

```text
"single line  " => "single line"
"single line\t" => "single line"
"single line  \n" => "single line\n"
"single line  \r\n" => "single line\r\n"
```

No final newline is added. The replacement only removes the matched spaces or tabs.

[user]

[assistant]
A compact Node helper for the exact requirement is:

```js
function stripTrailingSpacesAndTabs(text) {
  return text.replace(/[ \t]+(?=\r?$)/gm, "");
}
```

Quick smoke test:

```js
const input =
  "  INFO  started\t\r\n" +
  "\tDEBUG value=42  \t\n" +
  "WARN internal  spaces  \r\n" +
  "DONE";

const expected =
  "  INFO  started\r\n" +
  "\tDEBUG value=42\n" +
  "WARN internal  spaces\r\n" +
  "DONE";

const actual = stripTrailingSpacesAndTabs(input);

console.log(actual === expected ? "PASS" : "FAIL");
console.log(JSON.stringify(actual));
```

Expected output:

```text
PASS
"  INFO  started\r\n\tDEBUG value=42\nWARN internal  spaces\r\nDONE"
```

Use `[ \t]+$` after normalizing all line endings to LF, or use `[ \t]+(?=\r?$)` when preserving mixed CRLF and LF endings matters.