0

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.

tanascius
  • 53,078
  • 22
  • 114
  • 136
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 2
    Any editor can do a search/replace, is that not enough? – Sjoerd Jun 29 '10 at 07:14
  • 3
    Appropriate for what? The exact escaping convention that must be used depends on the language... Shell script? Perl? Php? Python? Tcl? – psmears Jun 29 '10 at 07:16
  • 1
    Can you explain your requirements better? You probably want to escape other characters... what if your string contains a backslash, newlines, or others? How will you use the string? – Kobi Jun 29 '10 at 07:57

4 Answers4

2

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
  • 58,710
  • 11
  • 92
  • 121
2

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 Z
  • 128,184
  • 27
  • 255
  • 279
  • 1
    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 Jun 29 '10 at 16:38
  • 1
    Hmm, I think I misread the question as just asking about regular expressions. – David Z Jun 29 '10 at 17:58
1

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
  • 507,862
  • 82
  • 626
  • 550
-1

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 says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551