dnewman
dnewman

Reputation: 275

regex object not working like simple regex

Having trouble converting a simple regex to a regex object... using jquery replace.

var st =  "saturate(0.2)"

// this is working
alert(st.replace(/saturate\(.*?\)/, "saturate(0.5)")) 

// so why is this not working exactly the same?
var st =  "saturate(0.2)"
var regex = new RegExp("saturate\(.*?\)", "")
alert(st.replace(regex, "saturate(0.5)")) 

http://jsfiddle.net/y6wGw/

Upvotes: 1

Views: 44

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149040

You need to escape the \ characters inside a string literal:

var regex = new RegExp("saturate\\(.*?\\)", "")

Demonstration

Upvotes: 4

Related Questions