Javascript undefined behavior with string.replace
Posted
by epochwolf
on Stack Overflow
See other posts from Stack Overflow
or by epochwolf
Published on 2010-04-25T02:52:36Z
Indexed on
2010/04/25
3:03 UTC
Read the original article
Hit count: 406
JavaScript
|undefined-behavior
I've been messing around with string.replace and I noticed something very odd with Webkit and Firebug's javascript consoles.
I can repeat this behavior in a blank browser window. (Look at the first and last lines)
>>> "/literature?page=".replace(/page=/i, "page=2")
"/literature?page="
>>> "/literature?page=".replace("page=", "page=2")
"/literature?page=2"
>>> "/literature?page=".replace(/page=/, "page=2")
"/literature?page=2"
>>> "/literature?page=".replace(/page=/i, "page=2")
"/literature?page=2"
Just so nobody thinks I mistyped something, here are screenshots.
Firebug (3.0.14)
Webkit (Latest nightly as of this post's creation.)
© Stack Overflow or respective owner