I have been trying to get a couple similar regular expressions to work within a jQuery script that is using the .match() function.
The old version of the RegEx I was using worked but since 'upgrading it' to be more restrictive I now cannot get it to run successfully:
I am using this RegEx:
var termString = queryString.match('/(?<=' + queryTermIdentifier + ').*?(?=\+)+/g');
But it is producing this error:
Uncaught SyntaxError: Invalid regular expression: //(?<=term=).*?(?=+)+/g/: Nothing to repeat
at String.match (<anonymous>)
at ready-functions.js?200323021240:1361
at ready-functions.js?200323021240:1575
at ready-functions.js?200323021240:1624
Where lines 1361 is where the RegEx is used. Line 1575 is the end of the function which uses it. The last incident on 1624 is the end of the document and self-executing jQuery function.
I am using a couple of these and they are all pretty similar:
var termString = queryString.match('/(?<=' + queryTermIdentifier + ').*?(?=\+)+/g');
var categoryString = queryString.match('/(?<=' + queryCategoryIdentifier + ').*?(?=\+)+/g');
var tagString = queryString.match('/(?<=' + queryTagIdentifier + ').*?(?=$)+/g');
They are all variations on a look-back '?<=' for a string up until an instance of a '+' for the first two or the end of the line in the last one.
As the console outputs two '/'s I tried removing this, and it brings it down to one '/'. I have also tried escaping it and that also works.
Not to sure how to proceed as this works within the RegExr tool I used to write it: regexr.com/50trq
Edit (Second Attempt)
var queryString = window.location.search;
var queryTermString = '';
var queryCategoryString = '';
var queryTagString = '';
var queryTermIdentifier = 'term=';
var queryCategoryIdentifier = 'category=';
var queryTagIdentifier = 'tag=';
if (queryString.indexOf(queryTermIdentifier)) {
console.log(queryTermIdentifier + 'was found');
var myReg = new RegExp('(?<=' + queryTermIdentifier + ').*?(?=\+)', 'g');
var termStringArray = myReg.exec(queryString);
if (termStringArray.length) {
queryTermString = termStringArray.index;
}
}
I am trying this method, however I still receive the same error.
Edit 2
(Swap 'term' for 'phrase' add additional '\' to '+' so is '\+').
I have removed the term 'term' as this I think is reserved for some sort of WordPress functionality, so have replaced it with 'phrase'.
I have also added an additional '\' to the usage of '+' as requested.
Here is the updated relevant snippet:
var queryString = window.location.search;
console.log(queryString);
// This returns '?phrase=hello+category=world+tag=youokay'
if (queryString.length) {
var queryTermString = '';
var queryTermIdentifier = 'phrase=';
if (queryString.indexOf(queryTermIdentifier) > 0) {
console.log(queryTermIdentifier + 'was found in query string');
var myTermReg = new RegExp('/(?<=' + queryTermIdentifier + ').*?(?=&)', 'g');
var termStringArray = myTermReg.exec(queryString);
console.log(termStringArray);
// THIS IS ALWAYS NULL???
if (termStringArray.length > 0) {
queryTermString = termStringArray.index;
}
}
}
This seems to have allowed the code to execute an extra line or two. However, now the console returns
Uncaught TypeError: Cannot read property 'length' of null
Referencing the line:
if (termStringArray.length) {
I can't comprehend why this is the case.