简体   繁体   English

JavaScript String.replace()

[英]JavaScript String.replace()

The following replacement 以下更换

"index.html".replace('\.html$', '_fr.html');

returns "index.html", indicating that the first argument didn't match anything. 返回“index.html”,表示第一个参数与任何内容都不匹配。 However, if I remove the "$" 但是,如果我删除“$”

"index.html".replace('\.html', '_fr.html');

then the first argument matches and "index_fr.html" is returned. 然后第一个参数匹配,并返回“index_fr.html”。

Returning to the first example, can someone explain why ".html$" does not seem to match "index.html"? 回到第一个例子,有人可以解释为什么“.html $”似乎与“index.html”不匹配?

Because that's not a regular expression - regex literals in JavaScript look like: 因为这不是正则表达式 - JavaScript中的正则表达式文字看起来像:

/\.html$/

without quotes. 没有引号。 String.replace takes a string or a regular expression literal. String.replace采用字符串或正则表达式文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM