使用replaceText特殊字符时出现问题:[] [英] problems using replaceText for special characters: [ ]

查看:78
本文介绍了使用replaceText特殊字符时出现问题:[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用"[1]"向后替换"\ cite {foo123a}".到目前为止,我已经可以使用以下命令替换文本

I want to replace "\cite{foo123a}" with "[1]" and backwards. So far I was able to replace text with the following command

body.replaceText('.cite{foo}', '[1]');

但是我没有设法使用

body.replaceText('\cite{foo}', '[1]');
body.replaceText('\\cite{foo}', '[1]');

为什么?

我根本无法上班的反向转换

The back conversion I cannot get to work at all

body.replaceText('[1]', '\\cite{foo}');

这将仅替换"1"而不是[],这意味着[]被解释为正则表达式字符集,转义它们将无济于事

this will replace only the "1" not the [ ], this means the [] are interpreted as regex character set, escaping them will not help

body.replaceText('\[1\]', '\\cite{foo}');//no effect, still a char set
body.replaceText('/\[1\]/', '\\cite{foo}');//no matches

文档说明

不完全支持JavaScript正则表达式功能的子集,例如捕获组和模式修饰符.

A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers.

我可以找到受支持的内容的完整描述,而在某处却找不到?

Can I find a full description of what is supported and what not somewhere?

推荐答案

我对Google Apps脚本不熟悉,但这看起来像普通的正则表达式麻烦.

I'm not familiar with Google Apps Script, but this looks like ordinary regular expression troubles.

您的第二次转换不起作用,因为字符串文字'\ [1 \]''[1]'相同.您想将文本 \ [1 \] 引用为字符串文字,这意味着'\\ [1 \\]'.字符串文字中的斜杠没有相关含义;在这种情况下,您编写的模式与文本/1/匹配.

Your second conversion is not working because the string literal '\[1\]' is just the same as '[1]'. You want to quote the text \[1\] as a string literal, which means '\\[1\\]'. Slashes inside of a string literal have no relevant meaning; in that case you have written a pattern which matches the text /1/.

您的第一次转换不起作用,因为 {...} 表示量词,而不是直括号,因此您需要 \\\\ cite \\ {foo \\} .(这四个反斜杠是因为在正则表达式中匹配文字 \ \\ ,并使其成为字符串文字是 \\\\ —两个转义的反斜杠.)

Your first conversion is not working because {...} denotes a quantifier, not literal braces, so you need \\\\cite\\{foo\\}. (The four backslashes are because to match a literal \ in a regular expression is \\, and to make that a string literal it is \\\\ — two escaped backslashes.)

这篇关于使用replaceText特殊字符时出现问题:[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆