Skip to Content
JavaScript 经典实例:第三版
book

JavaScript 经典实例:第三版

by Adam D. Scott, Matthew MacDonald, Shelley Powers
September 2022
Intermediate to advanced
585 pages
8h 17m
Chinese
China Electric Power Press Ltd.
Content preview from JavaScript 经典实例:第三版
字符串和正则表达式
63
第一次调用
replaceAll()
方法返回一个新字符串,后面在这个新字符串上调用
replaceAll()
法又返回一个新字符串。在一个方法的返回值上调用方法是一种编
程技术,叫作方法串联(
method chaining
)。
2.9.3
讨论
在网页中插入原始文本必需转义
HTML
。如果不执行这一步操作,可能会留下安
全漏洞。其实,打算插入网页的所有文本都要进行转义,即使文本中可能不包含
HTML
实体(例如,代码中的字面量)。说不定有人会从什么地方修改代码,替换
文本值。
然而,自己动手转义
HTML
往往不是最好的方法。如果你明确知道自己创建的字符
串可能会把
HTML
标签与外部内容掺在一起,那就要进行转义。但是,最好不要使
用元素的
innerHTML
属性把文本插入网页,应该使用
textContent
属性。浏览器自
动转义
textContent
属性中的内容,无须我们自己再使用
String.replaceAll()
法了。
2.9.4
参考
使用
HTMLDOM
把文本内容插入网页的进一步说明见第
12
章。
2.10
使用正则表达式替换字符串中的模式
2.10.1
问题
你想在字符串中搜索一个模式,而不是具体的字符序列。然后你想把模式替换掉,
创建一个新字符串。
2.10.2
方案
你可以使用
String.replace()
String.replaceAll()
法,二者都支持正则表
达式。
64
2
正则表达式是定义文本模式的字符序列。正则表达式是一个标准,
JavaScript
和很多其他语言都有实现。正则表达式的简明句法见表 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript

JavaScript

T. J. Crowder
JavaScript

JavaScript

David Flanagan
JavaScript Patterns

JavaScript Patterns

Stoyan Stefanov

Publisher Resources

ISBN: 9787519869601