none
escape code not working RRS feed

  • Question

  • | where-object {$_.innerText -eq 'Todays`'s Deals'}

    I have tried escaping the quote with a back apostrophe and with two single quotes, either way the script blows up. Any suggestions?
    Tuesday, July 8, 2014 9:00 PM

Answers

  • A single quote is escaped by a single quote.

                        
    | where-object {$_.innerText -eq 'Today''s Deals'}

    Or you can use full quotes:

                        
    | where-object {$_.innerText -eq "Today's Deals"}


    ¯\_(ツ)_/¯


    Tuesday, July 8, 2014 9:58 PM

All replies

  • While I would recommend using single quotes whenever you can, there are times you'll need to use double quotes. Here's are some rules I live by.

    • If your string contain no quotes, surround the string with single quotes
    • If your string contains single quotes, surround the string with doubles quotes
    • If you string contains double quotes, surround your string with single quotes
    • If you string contains both single and double quotes, surround your string with double quotes and escape the inner double quotes


    Edit: Changed code window for image.


    Tuesday, July 8, 2014 9:09 PM
  • While I would recommend using single quotes whenever you can, there are times you'll need to use double quotes. Here's are some rules I live by...

    • If your string contains single quotes, surround the string with doubles quotes
    • If you string contains double quotes, surround your string with single quotes
    • If you string contains both, use double quotes and escape the inner double quotes

    Edit: Changed code window for image.






    And if you just don't want to mess with it, put it into a here-string and put whatever kinds of quotes you want, where ever you want them:

    @'
    Today's "Deals" are 'Super"
    '@
    
    Today's "Deals" are 'Super"
    


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, July 8, 2014 9:26 PM
    Moderator
  • A single quote is escaped by a single quote.

                        
    | where-object {$_.innerText -eq 'Today''s Deals'}

    Or you can use full quotes:

                        
    | where-object {$_.innerText -eq "Today's Deals"}


    ¯\_(ツ)_/¯


    Tuesday, July 8, 2014 9:58 PM
  • This is the only way it has worked in my script:

    $link1 = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -eq "Today`'s Deals"}

    The single quote is escaped by the backquote and enclosed by double quotes. The code does not work any other way.
    • Edited by cupboy1 Saturday, August 23, 2014 2:43 AM
    Saturday, August 23, 2014 2:42 AM
  • This is the only way it has worked in my script:

    $link1 = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -eq "Today`'s Deals"}

    The single quote is escaped by the backquote and enclosed by double quotes. The code does not work any other way.

    You just made that up for fun. 

    Here is proof of the truth...

    PS C:\scripts> $text="Today's Deals"
    PS C:\scripts> $text
    Today's Deals
    PS C:\scripts> $Text -eq "Today`'s Deals"
    True
    PS C:\scripts> $Text -eq "Today's Deals"
    True
    PS C:\scripts> $Text -eq 'Today''s Deals'
    True PS C:\scripts>

    Note that all three work in this scenario but only my methods will work in all scenarios so be careful of the escape in a string "eq".


    ¯\_(ツ)_/¯

    Saturday, August 23, 2014 3:12 AM