EzDevInfo.com

escaping interview questions

Top escaping frequently asked interview questions

How to escape text for regular expression in Java

Does Java have a built-in way to escape arbitrary text so that it can be included in a regular expression? For example, if my users enter "$5", I'd like to match that exactly rather than a "5" after the end of input.


Source: (StackOverflow)

How to properly escape quotes inside html attributes?

I have a drop down on a web page which is breaking when the value string contains a quote.

The value is "asd but in the DOM always appears as an empty string.

I have tried every way I know to escape the string properly but to no avail.

<option value=""asd">test</option>
<option value="\"asd">test</option>
<option value="&quot;asd">test</option>
<option value="&#34;asd">test</option>

Any idea how to render this on the page so the postback message contains the correct value?


Source: (StackOverflow)

Advertisements

Is there a PHP function that can escape regex patterns before they are applied?

Is there a PHP function that can escape regex patterns before they are applied?

I am looking for something along the lines of the C# Regex.Escape() function.


Source: (StackOverflow)

Can I convert a C# string value to an escaped string literal

In C#, can I convert a string value to a string literal, the way I would see it in code? I would like to replace tabs, newlines, etc. with their escape sequences.

If this code:

Console.WriteLine(someString);

produces:

Hello
World!

I want this code:

Console.WriteLine(ToLiteral(someString));

to produce:

\tHello\r\n\tWorld!\r\n

Source: (StackOverflow)

Escaping HTML strings with jQuery

Does anyone know of an easy way to escape HTML from strings in jQuery? I need to be able to pass an arbitrary string and have it properly escaped for display in an HTML page (preventing JavaScript/HTML injection attacks). I'm sure it's possible to extend jQuery to do this, but I don't know enough about the framework at the moment to accomplish this.


Source: (StackOverflow)

Can I escape a double quote in a verbatim string literal?

In a verbatim string literal (@"foo") in C#, backslashes aren't treated as escapes, so doing \" to get a double quote doesn't work. Is there any way to get a double quote in a verbatim string literal?

This understandably doesn't work:

string foo = @"this \"word\" is escaped";

Source: (StackOverflow)

How to replace a character by a newline in Vim?

I'm trying to replace each , in the current file by a new line:

:%s/,/\n/g 

But it inserts what looks like a ^@ instead of an actual newline. The file is not in DOS mode or anything.

What should I do?

EDIT: If you are curious, like me, check the question Why is \r a newline for Vim? as well.


Source: (StackOverflow)

Colorize logs in eclipse console

Is there a way to colorize parts of logs in the eclipse console. I know I could send to error and standard streams and color them differently but I'm more looking someting in the lines of ANSI escape codes (or anyother, HTML ?) where I could embed the colors in the string to have it colored in the logs.

It sure would help making the important bits stand out without resorting to weird layout, rather keep the layout to the log4j setups

here is an example of what I am looking for :

[INFO ] The grid is complete ....... false

where the bold parts would be in blue, this coloring can be controlled by the application to an extent. like so (tags are conceptual and arbitrary, but you get the idea):

log.info(String.format("The grid is complete ....... <blue>%s</blue>", isComplete ));


On a more general note it is the ability to embed meta information in the logs to help the presentation of these logs. Much like we tag web pages content to help the presentation of the information by CSS.


Source: (StackOverflow)

How to escape a JSON string containing newline characters using javascript?

I have to form a JSON string in which a value is having new line character. This has to be escaped and then posted using ajax call. Can any one suggest a way to escape the string with javascript. I am not using jQuery.


Source: (StackOverflow)

PHP sprintf escaping %

I want the following output:-

About to deduct 50% of € 27.59 from your Top-Up account.

when I do something like this:-

$variablesArray[0] = '€';
$variablesArray[1] = 27.59;
$stringWithVariables = 'About to deduct 50% of %s %s from your Top-Up account.';
echo vsprintf($stringWithVariables, $variablesArray);

But it gives me this error vsprintf() [function.vsprintf]: Too few arguments in ... because it considers the % in 50% also for replacement. How do I escape it?


Source: (StackOverflow)

Escape text for HTML

How do i escape text for html use in C#? I want to do

sample="<span>blah<span>"

and have

<span>blah<span>

show up as plain text instead of blah only with the tags part of the html :(. Using C# not ASP


Source: (StackOverflow)

Why shouldn't `'` be used to escape single quotes?

As stated in, When did single quotes in HTML become so popular? and Jquery embedded quote in attribute, the Wikipedia entry on HTML says the following:

The single-quote character ('), when used to quote an attribute value, must also be escaped as &#x27; or &#39; (should NOT be escaped as &apos; except in XHTML documents) when it appears within the attribute value itself.

Why shouldn't &apos; be used? Also, is &quot; safe to be used instead of &#34;?


Source: (StackOverflow)

How to execute a bash command stored as a string with quotes and asterisk

I try to execute the following command :

mysql AMORE -u username -ppassword -h localhost -e "SELECT  host  FROM amoreconfig"

I store it in a string :

cmd="mysql AMORE -u username -ppassword -h localhost -e\"SELECT  host  FROM amoreconfig\""

Test it :

echo $cmd
mysql AMORE -u username -ppassword -h localhost -e"SELECT host FROM amoreconfig"

Try to execute by doing :

$cmd

And I get the help page of mysql :

mysql  Ver 14.14 Distrib 5.1.31, for pc-linux-gnu (i686) using readline 5.1
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Usage: mysql [OPTIONS] [database]
(...)

I guess I am doing something plain wrong with the quotes but can't find out what is the problem.


Source: (StackOverflow)

Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence

sample code:

>>> import json
>>> json_string = json.dumps("ברי צקלה")
>>> print json_string
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"

The problem: it's not human readable. My (smart) users want to verify or even edit text files with JSON dumps. (and i'd rather not use XML)

Is there a way to serialize objects into utf-8 json string (instead of \uXXXX ) ?

this doesn't help:

>>> output = json_string.decode('string-escape')
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"

this works, but if any sub-objects is a python-unicode and not utf-8, it'll dump garbage:

>>> #### ok:
>>> s= json.dumps( "ברי צקלה", ensure_ascii=False)    
>>> print json.loads(s)   
ברי צקלה

>>> #### NOT ok:
>>> d={ 1: "ברי צקלה", 2: u"ברי צקלה" }
>>> print d
{1: '\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94', 
 2: u'\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94'}
>>> s = json.dumps( d, ensure_ascii=False, encoding='utf8')
>>> print json.loads(s)['1']
ברי צקלה
>>> print json.loads(s)['2']
××¨× ×¦×§××

i searched the json.dumps documentation but couldn't find something useful.

Edit - Solution(?):

i'll try to sum up the comments and answers by Martijn Pieters:

(edit: 2nd thought after @Sebastian's comment and about a year later)

  1. there might be no is a built-in solution in json.dumps.

  2. i'll have to convert all strings to UTF8 Unicode the object before it's being JSON-ed. i'll use Mark's function that converts strings recuresively in a nested object

  3. the example I gave depends too much on my computer & IDE environment, and doesn't run the same on all computers.

Thank you everybody :)


Source: (StackOverflow)

How to escape double quotes in title attribute

I am trying to use a string that contains double quotes in the title attribute of an anchor. So far I tried these:

<a rel='nofollow' href=".." title="Some \"text\"">Some text</a>
<!-- title looks like `Some \` --!>

and

<a rel='nofollow' href=".." title="Some &quot;text&quot;">Some text</a>
<!-- title looks like `Some ` --!>

Please note that using single quotes is not an option.


Source: (StackOverflow)