First of all I needed to solve how to parse the multiple selectors. As Oriol said I cannot parse multiple selectors with regex. This function does what I need.
String.prototype.findStrings = function (s = ',') {
var prev = null, prevEscaped = null, typq = null,
typp = null, depRB = 0, depSB = 0;
var obj = { length: 0,
results: { array: [], A: null, B: null, C: null }
};
var action = null;
if (s.constructor === Array)
{
action = 1;
}
else
{
if (typeof s === 'string' || s instanceof String)
action = 0;
}
if ( action === null)
return false;
for (var i=0; i<this.length; i++)
{
// not escaped:
if (!prevEscaped)
{ // not escaped:
switch (this[i])
{
case '\\':
prevEscaped = true;
prev = '\\'; // previous escaped
break;
case '"':
if (!typq)
{
typq = '"';
}
else if ( typq=='"' )
{ // end quotes
typq = null;
continue;
}
break;
case "'":
if (!typq)
{
typq = "'";
}
else if ( typq=="'" )
{ // end quotes
typq = null;
continue;
}
break;
}
}
else
{ // is escaped - do nothing
prevEscaped = false;
prev = null;
continue;
}
if (!typq) // no quotes block
{
switch (this[i])
{
case '(':
if (!typp)
typp = '('; // defines higher priority of () parenthesis
depRB++;
break;
case "[":
if (!typp)
typp = '[';
depSB++;
break;
case ")":
depRB--;
if (!depRB)
{
if ( typp == "(" )
{
typp = null; // end block of parenthesis
continue;
}
}
break;
case "]":
depSB--;
if (!depSB)
{
if ( typp == "[" )
{
typp = null; // end block of parenthesis
continue;
}
}
break;
}
}
if (typp) // commas inside block of parenthesis of higher priority are skipped
continue;
if (!action) // Separate by string s
{
if ( this[i] == s )
{
obj.results.array.push(i);
obj.length++;
}
}
else
{
}
// Block of no quotes, no parenthesis follows
} // end for
// Last item
obj.results.array.push(i);
obj.length++;
return obj;
};
/* Return array of strings */
String.prototype.splitFromArray = function (arr, autotrim = true) {
var prev = 0, results = [];
for (var i = 0; i<arr.length; i++)
{
var s = "";
s = this.substring(prev+1,arr[i]).trim();
results.push( s );
prev = arr[i];
}
return results;
};
This is implemented like this:
var test = '#a > :matches(.b, #c) , input.match(title="\\City \\"of ' + "'" + 'silly' + "'" +' people.", data=' + "'alpha=" + '"' + "string" + '"' + ",beta=2'" + ' value="New York"), article.class1 ul li';
var separators = test.findStrings();
console.log(separators);
var myArr = test.splitFromArray(separators.results.array);
console.log(myArr);
Sorry that the selectors are not valid in the example but this just illustrates parsing. You can edit it to get valid selectors.
To add the search rules will not be hard.