console
let token = (function () {
let inline = {
comment: /^\/{2,}.*/,
reg: /^\/.+\//,
identifier: /^[a-zA-Z_$]+[a-zA-Z0-9_$]+/,
str: /^[`'"]+[^`"']*[`'"]+/,
char: /^[^\~\!@#\$\%\^\&\*\(\)\_\-\+\=\[\]\{\}\:\;\<\,\>\.\?\/\\\|\s]+/,
space: /^\s+/,
punct: /^[\~\!@#\%\^\&\*\(\)\-\+\=\[\]\{\}\:\;\<\,\>\.\?\/\\\|]+/,
}
let rule = {
keywords: ['break', 'else', 'new', 'var',
'case', 'finally', 'return', 'void',
'catch', 'for', 'switch', 'while',
'continue', 'function', 'with',
'default', 'if', 'throw', 'delete', 'in',
'try', 'do', 'instranceof', 'typeof', 'let',
'const', 'debugger', 'class', 'typeof', 'break'],
identifier: /^[a-zA-Z_$]+[a-zA-Z0-9_$]*/,
string: /[`'"]+[^`"']*[`'"]+/,
numeric: /^[0-9]+$/,
}
function colorize(str) {
if (rule.keywords.indexOf(str) != -1) {
return 'keyword';
} else if (rule.numeric.test(str) || (str == 'true' || str == 'false')) {
return 'bool';
} else if (rule.identifier.test(str)) {
return 'identifier';
} else if (rule.string.test(str)) {
return 'str';
}
return 'common';
}
function escape(str) {
let color = colorize(str);
return `<span class="${color}">${str.replace(/\s/g, ' ')}</span>`;
}
function wrap(str) {
return `<span>${str}</span>`;
}
function wrapLine(str) {
return `<div class="line">${str}</div>`;
}
return function split(str) {
if (!str || typeof str != 'string') {
return false;
}
let substr = str
let cap = null
let arr = []
let len = 0;
let matches = '';
let n = 0;
while (substr) {
if ((cap = inline.comment.exec(substr))) {
matches = {
text: cap[0],
color: colorize(cap[0]),
type: "comment",
};
len = cap[0].length;
arr.push(matches);
substr = substr.slice(len);
}
if ((cap = inline.reg.exec(substr))) {
matches = {
text: cap[0],
color: colorize(cap[0]),
type: "regexp",
};
len = cap[0].length;
arr.push(matches);
substr = substr.slice(len);
}
if ((cap = inline.identifier.exec(substr))) {
matches = {
text: cap[0],
color: colorize(cap[0]),
type: "identifier",
};
len = cap[0].length;
arr.push(matches)
substr = substr.slice(len)
}
if ((cap = inline.str.exec(substr))) {
matches = {
text: cap[0],
color: colorize(cap[0]),
type: "string",
};
len = cap[0].length;
arr.push(matches)
substr = substr.slice(len)
}
if ((cap = inline.char.exec(substr))) {
matches = {
text: cap[0],
color: colorize(cap[0]),
type: "character",
};
len = cap[0].length;
arr.push(matches)
substr = substr.slice(len)
}
if ((cap = inline.space.exec(substr))) {
matches = {
text: cap[0],
color: colorize(cap[0]),
type: "space",
};
len = cap[0].length;
arr.push(matches)
substr = substr.slice(len)
}
if ((cap = inline.punct.exec(substr))) {
matches = {
text: cap[0],
color: colorize(cap[0]),
type: "punctuation",
};
len = cap[0].length;
arr.push(matches)
substr = substr.slice(len)
}
n++;
if (n > 1000) {
break;
}
}
return arr;
}
})();
console.log(token("/a/"))
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=, initial-scale=">
<meta http-equiv="X-UA-Compatible" content="">
<title></title>
</head>
<body>
</body>
</html>