简体   繁体   中英

JavaScript String.replace()

The following replacement

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

returns "index.html", indicating that the first argument didn't match anything. However, if I remove the "$"

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

then the first argument matches and "index_fr.html" is returned.

Returning to the first example, can someone explain why ".html$" does not seem to match "index.html"?

Because that's not a regular expression - regex literals in JavaScript look like:

/\.html$/

without quotes. String.replace takes a string or a regular expression literal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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