From 07f8e406bca7ed2358ecfa193db21ec7fb40c021 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 29 Jul 2015 16:51:34 +0800 Subject: [PATCH 1/2] [components][expression_language] Add doc for backslashes Additional backslashes are required to escape a backslash(``\``) in a string or regex because a string will be stripped by the lexer. This should be documented here, otherwise, user may feel confused about the unexpected behavior. | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | ~2.4 | Fixed tickets | n/a --- components/expression_language/syntax.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/components/expression_language/syntax.rst b/components/expression_language/syntax.rst index ba153905aa6..ef870ec07f0 100644 --- a/components/expression_language/syntax.rst +++ b/components/expression_language/syntax.rst @@ -181,6 +181,28 @@ Comparison Operators You must use parenthesis because the unary operator ``not`` has precedence over the binary operator ``matches``. + A backslash(``\``) must be escaped by 4 backslashes(``\\\\``) in a string and + 8 backslashes(``\\\\\\\\``) in a regex:: + + $language->evaluate('"\\\\"'); + // returns \ + + $language->evaluate('"a\\\\b" matches "/^a\\\\\\\\b$/"'); + // returns true + + Control characters must be defined as the escaped form of their escape sequences. + Otherwise, they will be replaced by spaces and ignored:: + + $language->evaluate('"a\nb"'); + // returns a b + + $language->evaluate('"a\\nb"'); + // returns a\nb + + This is because the backslashes in a string will be stripped by the + ``stripcslashes()`` function and the stripped slashes in a regex will be + stripped again by the regex engine. + Examples:: $ret1 = $language->evaluate( From 31d74e5d2577436e27f765bafb594678bb1348d0 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sun, 7 Feb 2016 11:17:12 +0100 Subject: [PATCH 2/2] Add a caution about backslash escaping --- components/expression_language/syntax.rst | 34 ++++++++--------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/components/expression_language/syntax.rst b/components/expression_language/syntax.rst index ef870ec07f0..c3140ace7a2 100644 --- a/components/expression_language/syntax.rst +++ b/components/expression_language/syntax.rst @@ -20,6 +20,18 @@ The component supports: * **booleans** - ``true`` and ``false`` * **null** - ``null`` +.. caution:: + + A backslash (``\``) must be escaped by 4 backslashes (``\\\\``) in a string + and 8 backslashes (``\\\\\\\\``) in a regex:: + + echo $language->evaluate('"\\\\"'); // prints \ + $language->evaluate('"a\\\\b" matches "/^a\\\\\\\\b$/"'); // returns true + + Control characters (e.g. ``\n``) in expressions are replaced with + whitespace. To avoid this, escape the sequence with a single backslash + (e.g. ``\\n``). + .. _component-expression-objects: Working with Objects @@ -181,28 +193,6 @@ Comparison Operators You must use parenthesis because the unary operator ``not`` has precedence over the binary operator ``matches``. - A backslash(``\``) must be escaped by 4 backslashes(``\\\\``) in a string and - 8 backslashes(``\\\\\\\\``) in a regex:: - - $language->evaluate('"\\\\"'); - // returns \ - - $language->evaluate('"a\\\\b" matches "/^a\\\\\\\\b$/"'); - // returns true - - Control characters must be defined as the escaped form of their escape sequences. - Otherwise, they will be replaced by spaces and ignored:: - - $language->evaluate('"a\nb"'); - // returns a b - - $language->evaluate('"a\\nb"'); - // returns a\nb - - This is because the backslashes in a string will be stripped by the - ``stripcslashes()`` function and the stripped slashes in a regex will be - stripped again by the regex engine. - Examples:: $ret1 = $language->evaluate(