- 习题
- RegexpGolf
- QuotingStyle
- NumbersAgain
习题
在做本章习题时,读者不可避免地会对一些正则表达式的莫名其妙的行为感到困惑,因而备受挫折。读者可以使用类似于 http://debuggex.com/ 这样的在线学习工具,将你想编写的正则表达式可视化,并试验其对不同输入字符串的响应。
RegexpGolf
Code Golf 是一种游戏,尝试尽量用最少的字符来描述特定程序。类似的,Regexp Golf 这种活动是编写尽量短小的正则表达式,来匹配给定模式(而且只能匹配给定模式)。
针对以下几项,编写正则表达式,测试给定的子串是否在字符串中出现。正则表达式匹配的字符串,应该只包含以下描述的子串之一。除非明显提到单词边界,否则千万不要担心边界问题。当你的表达式有效时,请检查一下能否让正则表达式更短小。
car和catpop和propferret、ferry和ferrari以
ious结尾的单词句号、冒号、分号之前的空白字符
多于六个字母的单词
不包含
e(或者E)的单词
需要帮助时,请参考本章总结中的表格。使用少量测试字符串来测试每个解决方案。
// Fill in the regular expressionsverify(/.../,["my car", "bad cats"],["camper", "high art"]);verify(/.../,["pop culture", "mad props"],["plop", "prrrop"]]);verify(/.../,["ferret", "ferry", "ferrari"],["ferrum", "transfer A"]);verify(/.../,["how delicious", "spacious room"],["ruinous", "consciousness"]);verify(/.../,["bad punctuation ."],["escape the period"]);verify(/.../,["hottentottententen"],["no", "hotten totten tenten"]);verify(/.../,["red platypus", "wobbling nest"],["earth bed", "learning ape", "BEET"]);function verify(regexp, yes, no) {// Ignore unfinished exercisesif (regexp.source == "...") return;for (let str of yes) if (!regexp.test(str)) {console.log(`Failure to match '${str}'`);}for (let str of no) if (regexp.test(str)) {console.log(`Unexpected match for '${str}'`);}}
QuotingStyle
想象一下,你编写了一个故事,自始至终都使用单引号来标记对话。现在你想要将对话的引号替换成双引号,但不能替换在缩略形式中使用的单引号。
思考一下可以区分这两种引号用法的模式,并手动调用replace方法进行正确替换。
let text = "'I'm the cook,' he said, 'it's my job.'";// Change this call.console.log(text.replace(/A/g, "B"));// → "I'm the cook," he said, "it's my job."
NumbersAgain
编写一个表达式,只匹配 JavaScript 风格的数字。支持数字前可选的正号与负号、十进制小数点、指数计数法(5e-3或1E10,指数前也需要支持可选的符号)。也请注意小数点前或小数点后的数字也是不必要的,但数字不能只有小数点。例如.5和5.都是合法的 JavaScript 数字,但单个点则不是。
// Fill in this regular expression.let number = /^...$/;// Tests:for (let str of ["1", "-1", "+15", "1.55", ".5", "5.","1.3e2", "1E-4", "1e+12"]) {if (!number.test(str)) {console.log(`Failed to match '${str}'`);}}for (let str of ["1a", "+-1", "1.2.3", "1+1", "1e4.5",".5.", "1f5", "."]) {if (number.test(str)) {console.log(`Incorrectly accepted '${str}'`);}}
