/SOLVED/g How to form a RegEx with a string variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

18th Feb 2019, 6:05 AM
Gordon
Gordon - avatar
3 Answers
+ 18
Here's a quick piece of code that might help with what you are looking for: https://code.sololearn.com/WUHz4JTLmnmq/#js
18th Feb 2019, 6:34 AM
David Carroll
David Carroll - avatar
+ 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 :)
20th Feb 2019, 11:39 AM
Seniru
Seniru - avatar
- 1
dt
25th Feb 2019, 10:51 AM
Alex