Extract all wordpress shortcode in jQuery object
Extract all wordpress shortcode in jQuery object
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="content">
[artc_table_product3...
CSS
#content{
padding:20px;
background-color:#eee;
}
#output{
padding:20px;
background-color:#ccc;
}
JavaScript
jQuery(function() {
var pageContent = jQuery('#content').html();
var data = {};
var data = parseShortCode(pageContent);
//console.log(data);
jQuery('#output').html('<pre>'+JSON.stringify(data,null,4)+'</pre>');
function parseShortCode(shortCode) {
var regex_for_match = /\[([\w-]+)([^]*?)(\/?)\](?:([^]*?)\[\/\1\s*\])?/g;
var count = 0;
var s=[];
while ((singleMatch = regex_for_match.exec(shortCode)) != null) {
//console.log(singleMatch);
innerContent = singleMatch[4];
var re = /(\s+|\W)|(\w+)/g;
var match;
var token;
var curAttribute = '';
var quoteChar;
var mode = 'NOT STARTED';
var parsedValue = {
name: '',
attributes: {},
content: '',
parsedContent:''
};
while ((match = re.exec(singleMatch)) != null) {
token = match[0];
switch (mode) {
case 'NOT STARTED':
if (token == '[') {
mode = 'GETNAME';
}
break;
case 'GETNAME':
if (!(/\s/.test(token))) {
if (token == "]"){
mode = 'COMPLETE';
}else{
parsedValue.name += token;
}
} else if (parsedValue.name) {
mode = 'PARSING';
}
break;
case 'PARSING':
if (token == "]") { mode = 'COMPLETE'; }
else if (token == "=") {
if (!curAttribute) throw ('invalid token: "' + token + '" encountered at ' + match.index);
else mode = 'GET ATTRIBUTE VALUE';
}
else if (!/\s/.test(token)) {
curAttribute += token;
} else if (curAttribute) {
mode = 'SET ATTRIBUTE'
}
break;
case 'SET ATTRIBUTE':
// these are always from match[1]
if (/\s/.test(token)) { parsedValue.attributes[curAttribute] = null; }
else if (token == '=') { mode = 'GET ATTRIBUTE VALUE'; }
else { throw ('invalid token: "' + token + '" encountered at ' + match.index); }
break;
case 'GET ATTRIBUTE VALUE':
if (!(/\s/.test(token)))...