简体   繁体   English

Crockford - 第8章 - 第86页

[英]Crockford - Chapter 8 - page 86

Crockford: "JavaScript: The Good Parts" Crockford:“JavaScript:好的部分”

Chapter 8: Methods 第8章:方法

RegExp regexp.exec(string) 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. 在第86页的最后有一个很好的例子,但我还不了解其中的一件小事。

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 \\/ . 在JavaScript字符串中, /\\/之间没有区别。

Inside a <script> element the sequence </script> will end the element. <script>元素内,序列</script>将结束元素。 To represent it inside a string as data, you can use <\\/script> . 要将其作为数据在字符串中表示,可以使用<\\/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. 以编程方式生成JavaScript字符串时,通常的做法是在HTML中使用时转义所有/字符以防止终止脚本。

This is just code that follows that pattern. 这只是遵循该模式的代码。 There is no actual benefit achieved with this particular code. 使用此特定代码没有实现任何实际好处。

Probably this was supposed to be displayed as text on the webpage, instead of being parsed by browser. 可能这应该在网页上显示为文本,而不是由浏览器解析。 Check this out . 看看这个

Two different behavior: 两种不同的行为:

<html><body bgcolor=linen><p>This is <b>bold</b>!</p></body></html>
<html><body bgcolor=linen><p>This is <b>bold<\/b>!<\/p><\/body><\/html>

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

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