Look at a relatively reliable example: determine whether element A starts with element B, and has two characters more than element A than B.
For example: when B is "ABC", A is "ABCDE" and the condition will be met.
In a specific case like this, a variable needs to be added to the regular expression used for judgment.
Using something like rule = /^+B+[0-9]{2}$/ will definitely cause an error. The variable B will be understood as a character of a regular expression, and an error will be reported if it cannot be understood.
So we thought of generating regular expressions through the constructor RegExp().
var B = "abc"
var A = "abcde"
var rule= new RegExp("^"+B+"\\S{2}$") //S为大写的
console.log(rule.test(A))
The final output result is not true, that is, the verification is successful!
Hope this article will help you ^_^.