I’m trying to match the text “foo” between the two brackets in this following statement:
Expected [foo] but Recieved [__]
I’m then trying to replace the text with “…”. When I run the following code I get an invalid grouping error in JavaScript. When I run the expression in Regexr it works the way I expect it to.
var text = $("#assertion").html().replace( /(?<=Expected \[).*(?=\] )/,"...");
Javascript regexes don’t understand lookbacks (the
?<=). You need to match theExpected [part explicitely:The $1 is to avoid retyping the “Expected [” part and I changed the rest of the regex a bit to avoid greedy matching with
.*http://jsfiddle.net/VcraW/2/