Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 835 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JavaScript - how to build string with escape character?

#1
I am writing `JScript` to be run using `Windows Script Host`.

Say I have a simple string variable:

s1 = '\n'

I want to build `s2` from two separated chars: `\` & `n`.
**naively** I would like to do:

s2 = '';
s2 += '\\';
s2 += 'n';
But this of course lead to `s1 != s2`

Can I build `s2` in such way that it has the same **interpreted meaning** as `s1`?

### Example:

WScript.Echo("1\n2")
var s;
s += '1';
s += '\\';
s += 'n';
s += '2';
WScript.Echo(s)

I'd wish both `WScript.Echo()` to print exactly the same thing.

### Note
I'm well aware that this question seems **completely idiotic**. I would probably think the same had I read it without knowing all the details. I don't expect anyone to understand the purpose . just curious to see whether it is feasible or do I need to re-think the whole thing.
Reply

#2
You can't, this is impossible. You cannot concatenate two or more characters together to yield **one character**, which is what `\n` is. It is a string containing one single character.
Reply

#3
You can't build it directly by appending, but you can take a string with escape sequences in it and parse it to a string with the escaped characters. In this case, you'd probably use `JSON.parse`:

var s1 = '\n',
s2 = '' + '\\' + 'n'; // '\\n'
console.log(s1 == JSON.parse('"' + s2 + '"')) // true
Reply

#4
It's still somewhat unclear to me what you're actually trying to accomplish. However, if you're saying that you have some arbitrary string like `var s1 = '\n\r\t';`, and you want to produce from that the _literal_ string `"\n\r\t"` so that when you parse the literal value you get a result that is equivalent to your original string, perhaps you can try something like this:

window.specialChars = {
8: "\\b", //backspace
9: "\\t", //tab
10: "\\n", //newline
12: "\\f", //form-feed
13: "\\r", //carriage return
39: "\\'", //single-quote
92: "\\\\" //literal backslash
};

function escapedString(source) {
if (! source) {
return undefined;
}

var result = "";
for (var index = 0; index < source.length; index++) {
var code = source.charCodeAt(index);
result += specialChars[code] ? specialChars[code] : String.fromCharCode(code);
}

return result;
};

[To see links please register here]



Of course there are a number of problems you may run into here. Particularly if your source string may contain non-ASCII characters or spurious escape sequences (for instance, `var s1 = '\q';` is valid JavaScript and will store a value of `"q"` in `s1`, and when processing that there's no way to tell that the original source string has a spurious `\` in it).

It may be that there's a better alternative to solving whatever underlying problem you're attempting to solve with this "unparse the string" approach.
Reply

#5
I actually have the need to do this today. In particular, I need it for building up octal and hexadecimal escape sequences of the form `\x39` or `\123`.

The method I'm using is simply to use an `eval`:

var str = eval('"\\x' + '3' + '9' +'"') // str === "9"

That's a reasonable generic solution. For octal and hex codes, it would be better to parse the string into an int and then build a string from that

var str = String.fromCharCode(parseInt('39', 16))
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through