Copy link to clipboard
Copied
Hi, there.
Curious question:
I have the following code:
psYSbody.nestedGrepStyles.add({
appliedCharacterStyle: csYSimporedPsukimMekor,
grepExpression: "\\(.+?\\)"});
Why does this only work if I have double-backslashes before "()" and "[]"?
If I only have a single backslash it will totally ignore the backslash.
Anyone could explain this quirk?
Hi
The "/" character is an escape character. For example if you want to alert "the "big" cat" with the word cat in quotes one way of doing it is alert("the \"big\" cat");
Because you are providing a string you need to escape the escape
Try the following 2 examples
alert("\(") // => (
alert("\\(") // => \(
What you want is the second example as the "correct" grep is \( and not (
Got it?
Trevor
Copy link to clipboard
Copied
Hi
The "/" character is an escape character. For example if you want to alert "the "big" cat" with the word cat in quotes one way of doing it is alert("the \"big\" cat");
Because you are providing a string you need to escape the escape
Try the following 2 examples
alert("\(") // => (
alert("\\(") // => \(
What you want is the second example as the "correct" grep is \( and not (
Got it?
Trevor
Copy link to clipboard
Copied
Thank you Trevor.
I'm used to the UI GREP codes, so I appreciate your short and sweet explanation that JS strings are different
Copy link to clipboard
Copied
Hi,
Another way to explain the double-escape trick is to consider those two distinct levels:
1. At the GREP (or regular expression) level, parentheses ( and ) are special symbols that control capturing groups.
So if you want to capture a left parenthesis as itself you need to use an escape sequence.
In GREP, the escape character is \ (backslash) so that the full escape sequence is \( (backslash + parenthesis.)
2. At the JavaScript level (which is the language you are using to send the above GREP command), the backslash \ is a special symbol when involved in a literal string. Indeed, that symbol is used to form escape sequences!
So if you want to provide the backslash as itself in a literal string, you need the following escape sequence, "\\" (backslash + backslash), which in fact represents a single backslash character.
Finally, in order to encode in JavaScript the full GREP pattern \( —that is, a backslash and a left parenthesis—you'll have to use the literal string "\\(".
[The reasoning is the same for square brackets.]
Hope that helps.
@+
Marc
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more