Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提交作业 #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/blink.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
var marked = require('marked')

module.exports = function (str) {
var md = marked(str)
// TODO
return null
let md = marked(str)
// TODO
let reg = /@@(\S+)@@/g
let res = md.replace(reg, function (word, $1) {
return '<blink>' + $1 + '</blink>'
})
return res
}
5 changes: 4 additions & 1 deletion lib/capture.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = function (str) {
// TODO
// TODO
let reg = /\d+/
let match = reg.exec(str)
return Array.isArray(match) ? match[0] : match
}
5 changes: 4 additions & 1 deletion lib/capture_breakword.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = function (str) {
// TODO
// TODO
let reg = /\bx=(\d+)\b/
let match = reg.exec(str)
return Array.isArray(match) ? match[1] : match
}
5 changes: 4 additions & 1 deletion lib/quantified_group.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = function (str) {
// TODO
// TODO
let reg = /^(\s?0x[a-fA-F0-9]{2}\s?){8}$/g
let match = reg.exec(str)
return Array.isArray(match) ? match[0] : match
}
8 changes: 7 additions & 1 deletion lib/quotes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module.exports = function (str) {
// TODO
let reg = /("\b.*?\b")+|""/g
let match, res = []
while (!!(match = reg.exec(str))) {
//exec使arr返回匹配的第一个,while循环一次将使re在g作用寻找下一个匹配。
res.push(match[0])
}
return res
}
286 changes: 286 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ describe('regex', function () {
})

it('可以捕获正整数 x 的值,末尾带有 x 的变量不应当被捕获', function () {
const f = require('../lib/capture_breakword')
const f = require('../lib/capture_breakword')

assert.equal(f('x=5'), '5', 'x=5')
assert.equal(f('abc x=5'), '5', 'abc x=5')
assert.equal(f('fox=123'), null, 'fox=123')
assert.equal(f('x=abc'), null, 'x=abc')
assert.equal(f('x=33qrs'), null, 'x=33qrs')
assert.equal(f('3x=33'), null, '3x=33')
assert.equal(f('beep x=123123 boop'), '123123', 'beep x=123123 boop')
})
assert.equal(f('x=5'), '5', 'x=5')
assert.equal(f('abc x=5'), '5', 'abc x=5')
assert.equal(f('fox=123'), null, 'fox=123')
assert.equal(f('x=abc'), null, 'x=abc')
assert.equal(f('x=33qrs'), null, 'x=33qrs')
assert.equal(f('3x=33'), null, '3x=33')
assert.equal(f('beep x=123123 boop'), '123123', 'beep x=123123 boop')
})

it(`匹配8位 hex 代码,以'0x'开头,后面跟着两个字符可以是大写'A-F',小写'a-f',或者任意数字`, function () {
const f = require('../lib/quantified_group')
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('regex', function () {

it(`将'@@...@@'markdown语法变成'<blink>...</blink>'`, function () {
const f = require('../lib/blink')

assert.equal(
f('@@whatever@@').trim(),
'<p><blink>whatever</blink></p>',
Expand Down