我无法理解以下示例servlet代码中的replaceAll函数中替换的文本模式 App Engine documentation of the Channel API 。
String token = channelService.createChannel(game.getChannelKey(userId));
// Index is the contents of our index.html resource, details omitted for brevity.
index = index.replaceAll("\\{\\{ token \\}\\}", token);
非常感谢任何能够阐明这一点的人!!
最佳答案
解构它:
首先,由于它是一个字符串,因此字符串中的任何反斜杠都必须进行转义。因此字符串 \\{\\{ token\\}\\}
等于正则表达式 \{\{ token\}\}
。
好的,那么这个表达式有什么作用呢? {
是正则表达式中的特殊字符,因此反斜杠表示 {
应按字面意思视为 {
字符,与 }
相同。
因此它只是查找文字字符串 {{ token }}
,并将其替换为 token
变量的内容。
关于java - 有人可以解释一下App Engine文档中的以下servlet代码中的replaceAll函数是如何工作的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22449218/