views:

44

answers:

4

Does anyone know of a free website or program which will take a string and render it quoted with the appropriate escape characters?

Let's say, for instance, that I want to quote

It's often said that "devs don't know how to quote nested "quoted strings"".

And I would like to specify whether that gets enclosed in single or double quotes. I don't personally care for any escape character other than backslash, but other's might.

+1  A: 

In Python, for enclosing in single quotes:

import re
mystr = """It's often said that "devs don't know how to quote nested "quoted strings""."""
print("""'%s'""" % re.sub("'", r"\'", mystr))

Output:

'It\'s often said that "devs don\'t know how to quote nested "quoted strings"".'

You could easily adapt this into a more general form, and/or wrap it in a script for command-line invocation.

Amber
+2  A: 

If none of the double quotes of the string is already escaped, you can simply do:

str = str.replace(/"/g, "\\\"");

Otherwise, you should check if it is already escaped and replace only if it isn't; You can use lookbehind for that. The following is what came to my mind first but it would fail for strings like escaped backslash followed by quotes \\" :(

str = str.replace(/(?<!\\)"/g, "\\\"");

The following makes sure that the second last character, if exists, is not a backslash.

str = str.replace(/(?<!(^|[^\\])\\)"/g, "\\\"");

Update: Just remembered that JavaScript doesn't support look-behind; you can use the same regex on a look-behind supporting regex engine like perl/php/.net etc.

Amarghosh
+2  A: 

Any decent regex library in any decent programming language will have a function to do this - not that it's hard to write one yourself (as the other answers have indicated). So having a separate website or program to do it would be mostly useless.

  • Perl has the quotemeta function
  • PCRE's C++ wrapper has a function RE::QuoteMeta (warning: giant file at that link) which does the same thing
  • PHP has preg_quote if you're using Perl-compatible regexes
  • Python's re module has an escape function
  • In Java, the java.util.regex.Pattern class has a quote method
  • Perl and most of the other regular expression engines based on Perl have metacharacters \Q...\E, meaning that whatever comes between \Q and \E is interpreted literally
  • Most tools that use POSIX regular expressions (e.g. grep) have an option that makes them interpret their input as a literal string (e.g. grep -F)
David Zaslavsky
These are all good suggestions, but it still depends on what the output will be used for: these will work for regular expressions, and sometimes for double-quoted strings, but generally not for single-quoted strings, for example...
psmears
Hmm, I think I misread the question as just asking about regular expressions.
David Zaslavsky
A: 

so, I guess the answer is "no". Sorry, guys, but I didn't learn anything that I don't know. Probably my fault for not phrasing the question correctly.

+1 for everyone who posted

Mawg