Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Why does GREP expression need two back-slashes?

Explorer ,
Jan 25, 2016 Jan 25, 2016

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?

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Mentor , Jan 26, 2016 Jan 26, 2016

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

Translate
Mentor ,
Jan 26, 2016 Jan 26, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 26, 2016 Jan 26, 2016
LATEST

Thank you Trevor.

I'm used to the UI GREP codes, so I appreciate your short and sweet explanation that JS strings are different

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 26, 2016 Jan 26, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines