- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
为什么这段代码打印出 AAC
而不是预期的 A$`C
?
console.log('ABC'.replace('B', '$`'));
==>
AAC
以及如何让它给出预期的结果?
最佳答案
插入文字 $
你必须通过 $$
,因为 $`
:
Inserts the portion of the string that precedes the matched substring.
console.log('ABC'.replace('B', "$$`"));
参见 documentation .
其他模式:
Pattern Inserts $$
Inserts a $
.$&
Inserts the matched substring. $`
Inserts the portion of the string that precedes the matched substring. $'
Inserts the portion of the string that follows the matched substring. $<em>n</em>
Where n
is a positive integer less than 100, inserts the _n
_th parenthesized submatch string, provided the first argument was aRegExp
object. Note that this is1
-indexed. If a groupn
is not present (e.g., if group is 3), it will be replaced as a literal (e.g.,$3
).$<<em>Name</em>>
Where Name
is a capturing group name. If the group is not in the match, or not in the regular expression, or if a string was passed as the first argument toreplace
instead of a regular expression, this resolves to a literal (e.g.,$<Name>
). Only available in browser versions supporting named capturing groups.
此外,我在上面发布的引用链接中还有更多内容。如果您仍然有任何问题或疑问,您可能可以在那里找到答案,上面的屏幕截图是从答案开头发布的链接中截取的。
值得一提的是,在我看来,任何与上述不匹配的模式不需要转义,因此$
不需要转义,同样的故事发生在$AAA
.
在上面的评论中,一位用户询问了为什么你需要“转义”$
与另一个$
:尽管我对此不是很确定,但我认为还值得指出的是,从我们上面所说的,任何无效模式都不会被解释,因此我认为(并且怀疑,此时)$$
是一种非常特殊的情况,因为它涵盖了您需要用美元符号后跟“模式锁定”字符替换匹配项的情况,例如刻度线 ( `
)(或者实际上是 &
)作为另一个)。
但是,在任何其他情况下,美元符号不需要转义,因此他们决定创建这样一个特定规则可能是有道理的,否则您需要逃脱 $
其他任何地方(我认为这可能对任何字符串对象产生影响,因为这意味着即使在 var a = "hello, $ hey this one is a dollar";
中,您也需要转义 $
)。
如果您仍然感兴趣并想阅读更多内容,请查看 regular-expressions.info还有这个JSFiddle更多案例。
关于javascript - 为什么 'ABC' .replace ('B' , '$` ') 给出 AAC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31187452/
我有两种结构,Header 和Session,它们都符合协议(protocol)TimelineItem。 我有一个 Array 由 TimelineItem 组成,如下所示: [Header1, S
这个问题在这里已经有了答案: Multiple assignment and evaluation order in Python (11 个答案) 关闭 6 年前。 我刚接触python所以想问你
我试图找到一种方法来在 R 中获取 A、A、A、A、B、B、B、B、B 的所有可能的唯一排列的列表。 组合最初被认为是获得解决方案的方法,因此组合的答案。 最佳答案 我认为这就是你所追求的。 @bil
我怎样才能将两个给定的向量混合成一个新的向量,它以交替的顺序保存它们的值。 (f [a a] [b b]) ; > [a b a b] 这是我想到的: (flatten (map vector [:a
这是我的第一个问题,我开始学习Python。之间有区别吗: a, b = b, a + b 和 a = b b = a + b 当您在下面的示例中编写它时,它会显示不同的结果。 def fib(n):
这个问题在这里已经有了答案: Why is there an injected class name? (1 个回答) 12 个月前关闭。 我不知道如何解释: namespace A { struct
我尝试了一些代码来交换 Java 中的两个整数,而不使用第三个变量,使用 XOR。 这是我尝试过的两个交换函数: package lang.numeric; public class SwapVars
假设类 B 扩展类 A,并且我想为 B 声明一个变量。什么更有效?为什么? B b或 A b . 最佳答案 您混淆了两个不同的概念。 class B extends A { } 意味着B 是 A .
我不确定这个问题的标题是什么,这也可能是一个重复的问题。所以请相应地指导。 我是 python 编程的新手。我有这个简单的代码来生成斐波那契数列。 1: def fibo(n): 2: a =
我在谷歌上搜索了有关 dynamic_cast 的内容,我发现显式地将基类对象转换为派生类指针可能是不安全的。但是当我运行一些示例代码来检查它时,我没有收到任何错误。请在下面找到我的代码: class
这个问题在这里已经有了答案: What is this weird colon-member (" : ") syntax in the constructor? (14 个答案) 关闭 8 年前。
在不重现产生非整数值的表达式的情况下实现以下目标的惯用方法是什么(在我的真实情况下,该值是在我不想重现的冗长查询之后计算为百分比的): SELECT * FROM SomeTable WHERE 1/
在析构中,这两个代码的结果确实不同。我不确定为什么。 提示说 const [b,a] = [a,b] 将导致 a,b 的值为 undefined (从左到右的简单分配规则)。我不明白为什么会这样。 l
C++ Templates - The Complete Guide, 2nd Edition介绍max模板: template T max (T a, T b) { // if b < a th
我最近开始学习代码(Java),并根据第 15.17.3 节在 Oracle 网站上查找了模运算符。以下链接: http://docs.oracle.com/javase/specs/jls/se8/
无法理解以下行为。 d1 := &data{1}; 的区别d1 和 d2 := 数据{1}; &d1。两者都是指针,对吧?但他们的行为不同。这里发生了什么 package main import "f
这个问题在这里已经有了答案: How to make loop infinite with "x = y && x != y"? (4 个回答) How can i define variables
在我的程序中,当我调试我的代码时,它似乎在我生成的代码中的某处 X1=['[a,a,a]','[b,b,b]'] 还有我生成的其他地方 X2=[[a,a,a],[b,b,b]] 当我想添加这两个列表然
我试图使用递归将两个整数相乘,并意外编写了这段代码: //the original version int multiply(int a, int b) { if ( !b ) retu
我有一个列表中数字之间所有可能的操作组合: list = ['2','7','8'] 7+8*2 8+7*2 2*8+7 2+8*7 2-8*7 8-2/7 etc 我想知道是否可以说像 ('7*2+
我是一名优秀的程序员,十分优秀!