mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-09 21:01:20 -04:00
125 lines
2.8 KiB
Markdown
125 lines
2.8 KiB
Markdown
---
|
|
id: 594faaab4e2a8626833e9c3d
|
|
title: 使用转义标记字符串
|
|
challengeType: 1
|
|
forumTopicId: 302338
|
|
dashedName: tokenize-a-string-with-escaping
|
|
---
|
|
|
|
# --description--
|
|
|
|
Write a function or program that can split a string at each non-escaped occurrence of a separator character.
|
|
|
|
它应该接受三个输入参数:
|
|
|
|
<ul>
|
|
<li>The <strong>string</strong></li>
|
|
<li>The <strong>separator character</strong></li>
|
|
<li>The <strong>escape character</strong></li>
|
|
</ul>
|
|
|
|
它应该输出一个字符串列表。
|
|
|
|
拆分规则:
|
|
|
|
<ul>
|
|
<li>The fields that were separated by the separators, become the elements of the output list.</li>
|
|
<li>即使在开始和结束时,也应保留空字段。</li>
|
|
</ul>
|
|
|
|
转义规则:
|
|
|
|
<ul>
|
|
<li>"Escaped" means preceded by an occurrence of the escape character that is not already escaped itself.</li>
|
|
<li>当转义字符位于没有特殊含义的字符之前时,它仍然算作转义字符(但不会做任何特殊的事情)。</li>
|
|
<li>用于转义某些内容的每次出现的转义字符不应成为输出的一部分。</li>
|
|
</ul>
|
|
|
|
证明您的函数满足以下测试用例:
|
|
|
|
给定字符串
|
|
|
|
<pre>one^|uno||three^^^^|four^^^|^cuatro|</pre>
|
|
|
|
并使用 `|` 作为分隔符和 `^` 作为转义字符,你的函数应输出以下数组:
|
|
|
|
<pre> ['one|uno', '', 'three^^', 'four^|cuatro', '']
|
|
</pre>
|
|
|
|
# --hints--
|
|
|
|
`tokenize` 应该是一个函数。
|
|
|
|
```js
|
|
assert(typeof tokenize === 'function');
|
|
```
|
|
|
|
`tokenize` 应该返回一个数组。
|
|
|
|
```js
|
|
assert(typeof tokenize('a', 'b', 'c') === 'object');
|
|
```
|
|
|
|
`tokenize('one^|uno||three^^^^|four^^^|^cuatro|', '|', '^')` 应该返回 `['one|uno', '', 'three^^', 'four^|cuatro', '']`
|
|
|
|
```js
|
|
assert.deepEqual(tokenize(testStr1, '|', '^'), res1);
|
|
```
|
|
|
|
`tokenize('a@&bcd&ef&&@@hi', '&', '@')` 应该返回 `['a&bcd', 'ef', '', '@hi']`
|
|
|
|
```js
|
|
assert.deepEqual(tokenize(testStr2, '&', '@'), res2);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --after-user-code--
|
|
|
|
```js
|
|
const testStr1 = 'one^|uno||three^^^^|four^^^|^cuatro|';
|
|
const res1 = ['one|uno', '', 'three^^', 'four^|cuatro', ''];
|
|
|
|
// TODO add more tests
|
|
const testStr2 = 'a@&bcd&ef&&@@hi';
|
|
const res2 = ['a&bcd', 'ef', '', '@hi'];
|
|
```
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function tokenize(str, sep, esc) {
|
|
return true;
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
// tokenize :: String -> Character -> Character -> [String]
|
|
function tokenize(str, charDelim, charEsc) {
|
|
const dctParse = str.split('')
|
|
.reduce((a, x) => {
|
|
const blnEsc = a.esc;
|
|
const blnBreak = !blnEsc && x === charDelim;
|
|
const blnEscChar = !blnEsc && x === charEsc;
|
|
|
|
return {
|
|
esc: blnEscChar,
|
|
token: blnBreak ? '' : (
|
|
a.token + (blnEscChar ? '' : x)
|
|
),
|
|
list: a.list.concat(blnBreak ? a.token : [])
|
|
};
|
|
}, {
|
|
esc: false,
|
|
token: '',
|
|
list: []
|
|
});
|
|
|
|
return dctParse.list.concat(
|
|
dctParse.token
|
|
);
|
|
}
|
|
```
|