-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtext.js
35 lines (33 loc) · 871 Bytes
/
text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Created by youngwind on 16/9/6.
*/
/**
* 将文本节点如"{{user.name}}1111",解析成["{{user.name}}","1111"]两个节点
* @param text {String} 例如 "{{user.name}}1111"
*/
exports.parse = function (text) {
let tokens = [];
let tagRE = /\{?\{\{(.+?)\}\}\}?/g;
let match, index, value, lastIndex = 0;
while (match = tagRE.exec(text)) {
index = match.index;
if (index > lastIndex) {
tokens.push({
value: text.slice(lastIndex, index)
});
}
index = match.index;
value = match[1];
tokens.push({
tag: true,
value: value.trim()
});
lastIndex = index + match[0].length;
}
if (lastIndex < text.length - 1) {
tokens.push({
value: text.slice(lastIndex)
});
}
return tokens;
};