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).
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:
the actual value is
(
\tis a tab)You’d have to escape
\here as well:So before you think about a regular expression, double check the actual input.