Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  73] [ 4]  / answers: 1 / hits: 89443  / 14 Years ago, wed, march 2, 2011, 12:00:00

I am trying to create something similar to this:



var regexp_loc = /e/i;


except I want the regexp to be dependent on a string, so I tried to use new RegExp but I couldn't get what i wanted.



Basically I want the e in the above regexp to be a string variable but I fail with the syntax.



I tried something like this:



var keyword = something;

var test_regexp = new RegExp(/ + keyword + /i);


Basically I want to search for a sub string in a larger string then replace the string with some other string, case insensitive.



regards,
alexander


More From » regex

 Answers
24

You need to pass the second parameter:



var r = new RegExp(keyword, i);


You will also need to escape any special characters in the string to prevent regex injection attacks.


[#93494] Tuesday, March 1, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
makaylahk

Total Points: 166
Total Questions: 94
Total Answers: 117

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
;