Crockford: "JavaScript: The Good Parts"
Chapter 8: Methods
RegExp regexp.exec(string)
At the very end of page 86 there is a nice example but I don't yet understand one small thing in it.
var text = '<html><body bgcolor=linen><p>' +
'This is <b>bold<\/b>!<\/p><\/body><\/html>';
Why are the / (forward slashes) escaped here?
I tried the example without them, seems to work just fine.
In a JavaScript string, there is no difference between / and \/.
Inside a <script> element the sequence </script> will end the element. To represent it inside a string as data, you can use <\/script>.
When generating JavaScript strings programatically, it is a common practise to escape all / characters as a defence against terminating scripts when used inside HTML.
This is just code that follows that pattern. There is no actual benefit achieved with this particular code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With