escaping interview questions
Top escaping frequently asked interview questions
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)
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=""asd">test</option>
<option value=""asd">test</option>
Any idea how to render this on the page so the postback message contains the correct value?
Source: (StackOverflow)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
there might be no is a built-in solution in json.dumps.
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
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)
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 "text"">Some text</a>
<!-- title looks like `Some ` --!>
Please note that using single quotes is not an option.
Source: (StackOverflow)