Reputation: 5166
I had the same question with $$
signs (question with $$), but can't get it to work (same code) with \[
and \]
signs. I need regex pattern to recognize when I'm inside those signs (I can have multiple ones in the same text, they all should be covered).
text.replace(/\$.*?\$/g, function(m, n){
if(caret > n && caret < (n + m.length)){
alert("BOOM");
}
});
So /\$.*?\$/g
should become something else so it recognizes when I'm inside \[
and \]
signs. Regular replacing doesn't work (/\\[.*?\\]/g
).
Also \[some\thing\]
should work - it should select some\thing
(\ shouldn't make any troubles).
Upvotes: 0
Views: 3069
Reputation: 816790
If you want to match \[
literally, you have to escape both characters, \
and [
, thus it becomes \\\[
(the same for \]
).
In general: Whenever you want to match a character with a special meaning, you have to escape it with \
.
But note that the backslash is the escape character in strings as well. So if you defined a string as:
"\[some\thing\]"
the actual value is
"[some hing]"
(\t
is a tab)
You'd have to escape \
here as well:
"\\[some\\thing\\]"
// "\[some\thing\]"
So before you think about a regular expression, double check the actual input.
Upvotes: 2