Reputation: 3453
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 \[).*(?=\] )/,"...");
http://jsfiddle.net/bittersweetryan/VcraW/
Upvotes: 1
Views: 593
Reputation: 69984
Javascript regexes don't understand lookbacks (the ?<=
). You need to match the Expected [
part explicitely:
replace(/(Expected \[)[^\]]*/, "$1...")
The $1 is to avoid retyping the "Expected [" part and I changed the rest of the regex a bit to avoid greedy matching with .*
Upvotes: 6
Reputation: 93086
Because javascript does not support look behind assertions.
You can work around by putting this in a capturing group and using this in the replacement
.replace( /(Expected \[).*(?=\] )/,"$1...");
The content of the first pair of brackets is stored in the capturing group 1 and you can get this back using $1
Upvotes: 3