JavaScript inserting a variable into a regex for dynamic compares

So I'm working with Cypress and I'm attempting to understand how to verify a URL via regex matching; but I want to use several character sets in the regex, and I want to add a variable as well. Not sure if this is possible but many of the examples I see online aren't offering help.

The main part of the URL to check looks like: /quote/4/8942-4355-p664516?isOptionSelected=false
The block of bolded characters in the middle (8942-4355-p664516) is the changing variable that I need to insert
A normal expression for this would be: /[\S]*\/quote\/\d\/8942-4355-p664516\?isOptionSelected=[\S]*/

As you can see there's a lot of variability, and that changing variable could be taken up with another [/S]* - except that I need to validate that exactly, as part of the URL.

The test errors out if I just create the expression on the fly because it comes out as a string and using something like new RegExp malforms it. Is there a way to generate a regex like this, while inserting that variable into it on the fly?
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
So I'm working with Cypress and I'm attempting to understand how to verify a URL via regex matching; but I want to use several character sets in the regex, and I want to add a variable as well. Not sure if this is possible but many of the examples I see online aren't offering help.

The main part of the URL to check looks like: /quote/4/8942-4355-p664516?isOptionSelected=false
The block of bolded characters in the middle (8942-4355-p664516) is the changing variable that I need to insert
A normal expression for this would be: /[\S]*\/quote\/\d\/8942-4355-p664516\?isOptionSelected=[\S]*/

As you can see there's a lot of variability, and that changing variable could be taken up with another [/S]* - except that I need to validate that exactly, as part of the URL.

The test errors out if I just create the expression on the fly because it comes out as a string and using something like new RegExp malforms it. Is there a way to generate a regex like this, while inserting that variable into it on the fly?

It's been a while since I worked on regexs in JavaScript, but generally the way you do this in most programming languages is to build the regex as a string and then pass it into the regex engine. Pass something like "/[\\S]*/quote/\\d/" + varaible + "\?isOptionSelected=[\\S]*" to the new. Just remember that you have to escape your backslashes to have them survive through two levels of parsing.

Alternately, have you considered just notting? Rather than matching a regular expression that contains the varying part, use a capture group \[/S]*\/quote\/\d\/([^?]*)\?isOptionSelected=[\S]* then if the regex matches compare the caught string to the bolded characters and see if they match.
 
Alternately, have you considered just notting? Rather than matching a regular expression that contains the varying part, use a capture group \[/S]*\/quote\/\d\/([^?]*)\?isOptionSelected=[\S]* then if the regex matches compare the caught string to the bolded characters and see if they match.
Thank you Lt; this was the reasonable answer. Verify the shape/majority of the URL via regex, and if that's good, then as long as it contains that variable string it is virtually certain that the redirect is correct.

Funny, when searching around for solutions online I came across a quote from Jamie Zawinski back in '97: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. Seems like fair statement, but I really like using them. ;)
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
Alternately, have you considered just notting? Rather than matching a regular expression that contains the varying part, use a capture group \[/S]*\/quote\/\d\/([^?]*)\?isOptionSelected=[\S]* then if the regex matches compare the caught string to the bolded characters and see if they match.
Thank you Lt; this was the reasonable answer. Verify the shape/majority of the URL via regex, and if that's good, then as long as it contains that variable string it is virtually certain that the redirect is correct.

Funny, when searching around for solutions online I came across a quote from Jamie Zawinski back in '97: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. Seems like fair statement, but I really like using them. ;)

Regular expressions are great, but remember, the objective is parsing, that means extracting information from a string. Capture groups are usually how you deal with variability. And they are but the most basic mechanism for parsing, excellent if the thing you are parsing is regular, but insufficient otherwise.
 

grahamb

Ars Praefectus
3,893
Subscriptor
It's been a while since I worked on regexs in JavaScript, but generally the way you do this in most programming languages is to build the regex as a string and then pass it into the regex engine. Pass something like "/[\\S]*/quote/\\d/" + varaible + "\?isOptionSelected=[\\S]*" to the new. Just remember that you have to escape your backslashes to have them survive through two levels of parsing.

This will work; the RegExp constructor can take a string-representation of the expression as input. I wouldn't use string concatenation though; assuming at least an ES6 environment, you can use Template Literals ("backtick strings") which give you inline interpolation.

Code:
let regexp = new RegExp(`/[\\S]*/quote/\\d/${variable}\?isOptionSelected=[\\S]*`)