+ 15
/SOLVED/g How to form a RegEx with a string variable?
For example, The variable value is "art" I need a RegEx /art*/ So that I can string.search(RegEx) In another way of describing the original problem, When I search for art I don't want Earth to be in the filtered result. How to do it?
3 Respostas
+ 18
Here's a quick piece of code that might help with what you are looking for:
https://code.sololearn.com/WUHz4JTLmnmq/#js
+ 3
Another way of converting raw string to a RegExp (with flags) is described below.
var raw = "\bart\b"; //regex pattern in a raw string format
var reg = new RegExp(raw, "gi") //converting the raw string to a regexp with the flags global and insensitive.
Now you can use it for string.search() method. Hope this helped more :)
- 1
dt