From 29078a49482490cf13361120f12dcbdf69ba2d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E8=81=AA?= <469376974@qq.com> Date: Thu, 4 Apr 2019 19:01:37 +0800 Subject: [PATCH 1/2] commit --- lib/capture.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/capture.js b/lib/capture.js index a08ee57..0ae7dc3 100644 --- a/lib/capture.js +++ b/lib/capture.js @@ -1,3 +1,6 @@ module.exports = function (str) { - // TODO + // TODO + let reg = /(\d+)/ + let match = str.match(reg) + return Array.isArray(match) ? match[1] : match } \ No newline at end of file From 44d291114a368c2292e60740626803ae69180723 Mon Sep 17 00:00:00 2001 From: Mr-onions Date: Sun, 7 Apr 2019 14:31:32 +0800 Subject: [PATCH 2/2] finish commit --- lib/blink.js | 10 +- lib/capture.js | 6 +- lib/capture_breakword.js | 5 +- lib/quantified_group.js | 5 +- lib/quotes.js | 8 +- package-lock.json | 286 +++++++++++++++++++++++++++++++++++++++ test/test.js | 20 +-- 7 files changed, 321 insertions(+), 19 deletions(-) create mode 100644 package-lock.json diff --git a/lib/blink.js b/lib/blink.js index c391a9f..4e36472 100644 --- a/lib/blink.js +++ b/lib/blink.js @@ -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 '' + $1 + '' + }) + return res } \ No newline at end of file diff --git a/lib/capture.js b/lib/capture.js index 0ae7dc3..9abad0c 100644 --- a/lib/capture.js +++ b/lib/capture.js @@ -1,6 +1,6 @@ module.exports = function (str) { // TODO - let reg = /(\d+)/ - let match = str.match(reg) - return Array.isArray(match) ? match[1] : match + let reg = /\d+/ + let match = reg.exec(str) + return Array.isArray(match) ? match[0] : match } \ No newline at end of file diff --git a/lib/capture_breakword.js b/lib/capture_breakword.js index a08ee57..08d7f88 100644 --- a/lib/capture_breakword.js +++ b/lib/capture_breakword.js @@ -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 } \ No newline at end of file diff --git a/lib/quantified_group.js b/lib/quantified_group.js index a08ee57..117fd17 100644 --- a/lib/quantified_group.js +++ b/lib/quantified_group.js @@ -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 } \ No newline at end of file diff --git a/lib/quotes.js b/lib/quotes.js index a08ee57..cfb5bae 100644 --- a/lib/quotes.js +++ b/lib/quotes.js @@ -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 } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..50c4a00 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,286 @@ +{ + "name": "exercise7", + "version": "0.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "browser-stdout": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.0.tgz", + "integrity": "sha1-81HTKWnTL6XXpVZxVCY9korjvR8=", + "dev": true + }, + "commander": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.9.0.tgz", + "integrity": "sha1-nJkJQXbhIkDLItbFFGCYQA/g99Q=", + "dev": true, + "requires": { + "graceful-readlink": ">= 1.0.0" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "debug": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.8.tgz", + "integrity": "sha1-5zFTHKLt4n0YgiJCfaF4IdaP9Pw=", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "diff": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.2.0.tgz", + "integrity": "sha1-yc45Okt8vQsFinJck98pkCeGj/k=", + "dev": true + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "glob": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.1.tgz", + "integrity": "sha1-gFIR3wT6rxxjo2ADBs31reULLsg=", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.2", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "graceful-readlink": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz", + "integrity": "sha1-TK+tdrxi8C+gObL5Tpo906ORpyU=", + "dev": true + }, + "growl": { + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.9.2.tgz", + "integrity": "sha1-Dqd0NxXbjY3ixe3hd14bRayFwC8=", + "dev": true + }, + "has-flag": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-1.0.0.tgz", + "integrity": "sha1-nZ55MWXOAXoA8AQYxD+UKnsdEfo=", + "dev": true + }, + "he": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", + "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=", + "dev": true + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "dev": true + }, + "json3": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/json3/-/json3-3.3.2.tgz", + "integrity": "sha1-PAQ0dD35Pi9cQq7nsZvLSDV19OE=", + "dev": true + }, + "lodash._baseassign": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/lodash._baseassign/-/lodash._baseassign-3.2.0.tgz", + "integrity": "sha1-jDigmVAPIVrQnlnxci/QxSv+Ck4=", + "dev": true, + "requires": { + "lodash._basecopy": "^3.0.0", + "lodash.keys": "^3.0.0" + } + }, + "lodash._basecopy": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/lodash._basecopy/-/lodash._basecopy-3.0.1.tgz", + "integrity": "sha1-jaDmqHbPNEwK2KVIghEd08XHyjY=", + "dev": true + }, + "lodash._basecreate": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash._basecreate/-/lodash._basecreate-3.0.3.tgz", + "integrity": "sha1-G8ZhYU2qf8MRt9A78WgGoCE8+CE=", + "dev": true + }, + "lodash._getnative": { + "version": "3.9.1", + "resolved": "https://registry.npmjs.org/lodash._getnative/-/lodash._getnative-3.9.1.tgz", + "integrity": "sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=", + "dev": true + }, + "lodash._isiterateecall": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/lodash._isiterateecall/-/lodash._isiterateecall-3.0.9.tgz", + "integrity": "sha1-UgOte6Ql+uhCRg5pbbnPPmqsBXw=", + "dev": true + }, + "lodash.create": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/lodash.create/-/lodash.create-3.1.1.tgz", + "integrity": "sha1-1/KEnw29p+BGgruM1yqwIkYd6+c=", + "dev": true, + "requires": { + "lodash._baseassign": "^3.0.0", + "lodash._basecreate": "^3.0.0", + "lodash._isiterateecall": "^3.0.0" + } + }, + "lodash.isarguments": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz", + "integrity": "sha1-L1c9hcaiQon/AGY7SRwdM4/zRYo=", + "dev": true + }, + "lodash.isarray": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/lodash.isarray/-/lodash.isarray-3.0.4.tgz", + "integrity": "sha1-eeTriMNqgSKvhvhEqpvNhRtfu1U=", + "dev": true + }, + "lodash.keys": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/lodash.keys/-/lodash.keys-3.1.2.tgz", + "integrity": "sha1-TbwEcrFWvlCgsoaFXRvQsMZWCYo=", + "dev": true, + "requires": { + "lodash._getnative": "^3.0.0", + "lodash.isarguments": "^3.0.0", + "lodash.isarray": "^3.0.0" + } + }, + "marked": { + "version": "0.3.19", + "resolved": "https://registry.npmjs.org/marked/-/marked-0.3.19.tgz", + "integrity": "sha512-ea2eGWOqNxPcXv8dyERdSr/6FmzvWwzjMxpfGB/sbMccXoct+xY+YukPD+QTUZwyvK7BZwcr4m21WBOW41pAkg==", + "dev": true + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + } + }, + "mocha": { + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-3.5.3.tgz", + "integrity": "sha512-/6na001MJWEtYxHOV1WLfsmR4YIynkUEhBwzsb+fk2qmQ3iqsi258l/Q2MWHJMImAcNpZ8DEdYAK72NHoIQ9Eg==", + "dev": true, + "requires": { + "browser-stdout": "1.3.0", + "commander": "2.9.0", + "debug": "2.6.8", + "diff": "3.2.0", + "escape-string-regexp": "1.0.5", + "glob": "7.1.1", + "growl": "1.9.2", + "he": "1.1.1", + "json3": "3.3.2", + "lodash.create": "3.1.1", + "mkdirp": "0.5.1", + "supports-color": "3.1.2" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "supports-color": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-3.1.2.tgz", + "integrity": "sha1-cqJiiU2dQIuVbKBf83su2KbiotU=", + "dev": true, + "requires": { + "has-flag": "^1.0.0" + } + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + } + } +} diff --git a/test/test.js b/test/test.js index 0bcfdc7..c97dbf6 100644 --- a/test/test.js +++ b/test/test.js @@ -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') @@ -68,7 +68,7 @@ describe('regex', function () { it(`将'@@...@@'markdown语法变成'...'`, function () { const f = require('../lib/blink') - + assert.equal( f('@@whatever@@').trim(), '

whatever

',