JavaScript - How to Show Escape Characters in a String

Javascript - How to show escape characters in a string?

If your goal is to have

str = "Hello\nWorld";

and output what it contains in string literal form, you can use JSON.stringify:

console.log(JSON.stringify(str)); // ""Hello\nWorld""

const str = "Hello\nWorld";
const json = JSON.stringify(str);
console.log(json); // ""Hello\nWorld""
for (let i = 0; i < json.length; ++i) {
console.log(`${i}: ${json.charAt(i)} (0x${json.charCodeAt(i).toString(16).toUpperCase().padStart(4, "0")})`);
}
.as-console-wrapper {
max-height: 100% !important;
}

JS show special ascii characters in a string

The key is to escape \n in a string replace.

let specialCharacters = [    {regex: /\n/g, replacement: '\\n'},    {regex: /\t/g, replacement: '\\t'}];
function escapeSpecialCharacters(str){ specialCharacters.forEach(c => { str = str.replace(c.regex, c.replacement); }); return str;}
console.log(escapeSpecialCharacters(`testtesttest1234`));

Not able to escape characters in my regex

Your string doesn't have any literal backslashes in it. For example, \tAgile,Scrum is equivalent to a tab character concatenated with Agile,Scrum. There are no backslashes in the string, so /\\/g, which matches a literal backslash, will not match anything.

You'll have to match the interpreted character(s) instead, like \t, and replace them with a literal backslash followed by a literal t:

var a = {"value": '[{"skill":"\tAgile,Scrum", "exp": 2}, {"skill": "Coding", "exp": 10 }]'};

a.value = a.value.replace(/\t/g, "\\t"); //handle special char

console.log(a);

Retain Special Characters In A String (Without Breaks)

It seems you are looking for escaping:

pushTestObj["myValue"] = "I call myself \"Joe\""; // these strings may have apostrophes or single quotes that are legit

pushTestObj["myValue"] = "/* He's \"different\" */ Sometimes David"; // these strings may have apostrophes, quotes, or even comments that are legit

A string can contain apostrophes or single quotes, no problem there. However you cannot type them in a string literal without escaping.

Comments have no special meaning inside a string.

It really helps if you get an texteditor with syntax highlighting, as it will quickly show you where a string starts and ends.

Javascript - Replacing the escape character in a string literal

If it's a literal, you need to escape the backslashes before Javascript sees them; there's no way around that.

var newpath = 'file:///C:\\funstuff\\buildtools\\viewer.html';
window.location = newpath;

If newpath is getting its value from somewhere else, and really does contain single backslashes, you don't need to double them up; but if you really wanted to for some reason, don't forget to escape the backslashes in the replace() call:

newpath.replace(/\\/g,"\\\\");

Why do you not have the option of properly escaping the backslashes before they are handled by Javascript? If the problem is that your Javascript source is being generated from some other scripting language that itself uses \ as an escape character, just add a level of escaping:

var newpath = 'file:///C:\\\\funstuff\\\\buildtools\\\\viewer.html';

Count the length of a string with special chars in Javascript

If you want the number of source characters (including the escape characters), there's no programmatic way to determine that, you'd have to do it by looking at the source code and counting.

The reason there's no way to do that programatically that once it's a string, it's a string, and there are many, many, many different ways the same string can be written using escape sequences.

For instance, these all define the same string:

var s1 = "v\xfb\"lgs\"kvjfywmut\x9cr";
var s2 = 'v\xfb"lgs"kvjfywmut\x9cr';
var s3 = "\x76\xfb\"lgs\"k\x76jfywmut\x9cr";

...and yet as you can see, the number of source characters differs.



Related Topics



Leave a reply



Submit