{
int pos = 0, lastPos = 0;
while ((pos = rx.indexIn(str, pos)) != -1)
{
out += str.mid(lastPos, pos - lastPos);
switch (rx.cap(1).at(0).unicode())
{
case '\r':
out += "\\r";
break;
case '\n':
out += "\\n";
break;
case '"':
out += "\\\"";
break;
case '\\':
out += "\\\\";
break;
}
pos++;
lastPos = pos;
}
out += str.mid(lastPos);
return out;
}
QString escapeJavascriptString(const QString & str)
{
QString out;
QRegExp rx("(\\r|\\n|\\\\|\")");
int pos = 0, lastPos = 0;
while ((pos = rx.indexIn(str, pos)) != -1)
{
out += str.mid(lastPos, pos - lastPos);
switch (rx.cap(1).at(0).unicode())
{
case '\r':
out += "\\r";
break;
case '\n':
out += "\\n";
break;
case '"':
out += "\\\"";
break;
case '\\':
out += "\\\\";
break;
}
pos++;
lastPos = pos;
}
out += str.mid(lastPos);
return out;
}
To copy to clipboard, switch view to plain text mode
Bookmarks