Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (!Array.prototype.newStyle) {
- Array.prototype.newStyle = function() {
- this.push({'name' : '', 'string' : ''});
- };
- }
- if (!Array.prototype.last) {
- Array.prototype.__defineGetter__('last', function() {
- return this[this.length - 1];
- });
- }
- if (!String.prototype.styleTokenizer) {
- String.prototype.styleTokenizer = function() {
- var styles = [];
- styles.newStyle();
- var state = 'ReadString';
- for (var symbol of this) {
- switch (state) {
- case 'ReadString':
- if (symbol == '[') {
- styles.newStyle();
- state = 'ReadStyleHeader';
- } else
- styles.last.string += symbol;
- break;
- case 'ReadStyleHeader':
- if (symbol == ']')
- state = 'ReadStyledString';
- else
- styles.last.name += symbol;
- break;
- case 'ReadStyledString':
- if (symbol == '}') {
- styles.newStyle();
- state = 'ReadString';
- } else if (symbol != '{')
- styles.last.string += symbol;
- break;
- }
- }
- return styles;
- }}
- var styles = 'kek kek kek [bold]{ its a bold string } vanilla string [SuperDuper] { superduper string }'.styleTokenizer();
- styles.forEach(function(style) {
- console.log('style[' + style.name + '] : ' + style.string)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement