JavaScript 中包含转义序列的正则表达式

发布于 2024-12-21 10:06:12 字数 686 浏览 3 评论 0 原文

我有一个包含颜色转义序列的字符串,如下所示:

"white text \x1b[33m yellow text \x1b[32m green text"

现在我需要替换所有出现的某个转义序列。我只得到我要寻找的转义序列,这就是我所拥有的。据我所知,在 JavaScript 中替换所有出现的内容的唯一方法是使用正则表达式。

// replace all occurences of one sequence string with another
function replace(str, sequence, replacement) {
  // get the number of the reset colour sequence
  code = sequence.replace(/\u001b\[(\d+)m/g, '$1');
  // make it a regexp and replace all occurences with the start colour code
  return str.replace(new RegExp('\\u001b\\[' + code + 'm', 'g'), replacement);
}

因此,我得到了想要搜索的转义序列,然后使用正则表达式从该序列中获取数字,以构造另一个用于搜索转义序列的正则表达式。难道就没有更简单、更好的方法吗?

I have a string containing colour escape sequences, like that:

"white text \x1b[33m yellow text \x1b[32m green text"

Now I need to replace all occurences of a certain escape sequence. I only get the escape sequence I shall look for, that's what I have. As far as I know, the only way in JavaScript to replace all occurences of something is to use regular expressions.

// replace all occurences of one sequence string with another
function replace(str, sequence, replacement) {
  // get the number of the reset colour sequence
  code = sequence.replace(/\u001b\[(\d+)m/g, '$1');
  // make it a regexp and replace all occurences with the start colour code
  return str.replace(new RegExp('\\u001b\\[' + code + 'm', 'g'), replacement);
}

So, I am getting the escape sequence I want to search for, then I use a regular expression to get a number out of that sequence just to construct another regular expression that would search for the escape sequence. Isn't there an easier, nicer way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习惯成性 2024-12-28 10:06:12

如果你的问题是我所认为的,我认为更简单更好的方法就是转义你的模式并将其直接传递给 RegExp 构造函数,如我的这个老问题所示

如何在不需要转义所有内容的情况下进行全局字符串替换?

function escape(s) {
    return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\
amp;')
};

function replace_all(str, pattern, replacement){
    return str.replace( new RegExp(escape(pattern), "g"), replacement);
}

replace_all(my_text, "\x1b[33m", "REPLACEMENT")

If your problem is what I think it i, I think the easier and nicer way is just escaping your pattern and passing it directly to the RegExp constructor, as seen in this old question of mine

How do I do global string replace without needing to escape everything?

function escape(s) {
    return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\
amp;')
};

function replace_all(str, pattern, replacement){
    return str.replace( new RegExp(escape(pattern), "g"), replacement);
}

replace_all(my_text, "\x1b[33m", "REPLACEMENT")
℡寂寞咖啡 2024-12-28 10:06:12

OP 中的原始解决方案非常有效,并且在我看来只有两个问题。

  1. "code = ..." 语句需要一个 var - 按原样它会污染全局命名空间。
  2. "code = ..." 语句需要一些错误检查来处理错误的sequence 输入。

以下是我改进它的方法:

// replace all occurrences of one ANSI escape sequence with another.
function replace(str, sequence, replacement) {
  // Validate input sequence and extract color number.
  var code = sequence.match(/^\x1b\[(\d+)m$/);
  if (!code) {  // Handle invalid escape sequence.
    alert('Error! Invalid escape sequence');
    return str;
  }
  // make it a regexp and replace all occurrences with the start color code
  return str.replace(new RegExp('\\x1b\\[' + code[1] + 'm', 'g'), replacement);
}

The original solution in the OP is quite efficient and has only two problems as I see it.

  1. The "code = ..." statement needs a var - As-is it is polluting the global namespace.
  2. The "code = ..." statement needs some error checking to handle bad sequence input.

Here's how I'd improve it:

// replace all occurrences of one ANSI escape sequence with another.
function replace(str, sequence, replacement) {
  // Validate input sequence and extract color number.
  var code = sequence.match(/^\x1b\[(\d+)m$/);
  if (!code) {  // Handle invalid escape sequence.
    alert('Error! Invalid escape sequence');
    return str;
  }
  // make it a regexp and replace all occurrences with the start color code
  return str.replace(new RegExp('\\x1b\\[' + code[1] + 'm', 'g'), replacement);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文