, '_fr.html'); returns "index.html", which indicates that the ...">

JavaScript String.replace ()

Next replacement

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

returns "index.html", which indicates that the first argument matches nothing. 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"?

+3
source share
1 answer

Because it is not a regular expression - regular expression literals in JavaScript look like this:

/\.html$/

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

+5
source

Source: https://habr.com/ru/post/1774483/


All Articles