编写一个函数或程序,可以在分隔符的每个非转义事件中拆分字符串。
它应该接受三个输入参数:
字符串 分隔符字符 转义字符它应该输出一个字符串列表。
拆分规则:
由分隔符分隔的字段将成为输出列表的元素。应保留空字段,即使在开始和结束时也是如此。转义规则:
“Escaped”意味着出现一个尚未自行转义的转义字符。当转义字符位于没有特殊含义的字符之前时,它仍然被视为转义符(但不会做任何特殊操作)。用于转义某些内容的每次出现的转义字符都不应成为输出的一部分。证明您的函数满足以下测试用例:给定字符串
一个^ | UNO || 3 ^^^^ |四^^^ | ^夸|和使用
|作为分隔符和
^作为转义字符,您的函数应输出以下数组:
['one | uno',“,'three ^^','four ^ | quatro',”]
tokenize
是一个函数。
testString: 'assert(typeof tokenize === "function", "tokenize
is a function.");'
- text: tokenize
应该返回一个数组。
testString: 'assert(typeof tokenize("a", "b", "c") === "object", "tokenize
should return an array.");'
- text: 'tokenize("one^|uno||three^^^^|four^^^|^cuatro|", "|", "^")
应返回[“one | uno”,“”,“three ^^” ,“四个^ | cuatro”,“”]“)'
testString: 'assert.deepEqual(tokenize(testStr1, "|", "^"), res1, "tokenize("one^|uno||three^^^^|four^^^|^cuatro|", "|", "^")
should return ["one|uno", "", "three^^", "four^|cuatro", ""]");'
- text: 'tokenize("a@&bcd&ef&&@@hi", "&", "@")
应返回["a&bcd", "ef", "", "@hi"]
'
testString: 'assert.deepEqual(tokenize(testStr2, "&", "@"), res2, "tokenize("a@&bcd&ef&&@@hi", "&", "@")
should return ["a&bcd", "ef", "", "@hi"]
");'
```