How to render unicode escape sequences using javascript? - javascript

Below code,
document.writeln("Caracteres escapados {\u55e8\uff0c\u4f60\u597d\u5417}: "
+ "\u55e8\uff0c\u4f60\u597d\u5417");
expected output is \u55e8\uff0c\u4f60\u597d\u5417: 嗨,你好吗
Current output is: Caracteres escapados {嗨,你好吗}: 嗨,你好吗
What modification is required in the above code?

You need to escape your backslashes and remove the braces.
document.writeln("Caracteres escapados \\u55e8\\uff0c\\u4f60\\u597d\\u5417 "
+ "\u55e8\uff0c\u4f60\u597d\u5417");

Just escape the backslashes of the codes you want to show without translating:
document.writeln("Carácteres escapados \\u55e8\\uff0c\\u4f60\\u597d\\u5417: \u55e8\uff0c\u4f60\u597d\u5417");

Related

How do I format doc.writeln without using quotation marks

I receive an error in the console when I want to write a html doc with this syntax:
doc.writeln (" $e.html($(e).html().split(text).join('<span class='matching'>' + text + '</span>'));");
the error appear here:
join('<span class='matching'>'
exactly:
'<span class='
I can't put double quotation marks "" but only single quotes '' on "matching".
How can I write the code to overcome this error?
You need to escape the quotes that are inside other quotes. You also have to double the backslashes so that the backslashes will be treated literally inside the double quotes.
doc.writeln (" $e.html($(e).html().split(text).join('<span class=\\'matching\\'>' + text + '</span>'));");

Replace Single Quotes in Javascript String Support UTF-8

In my web application I get data from Instagram. Some full names have single quotes like:
♠ فال ورق ' تاروت ' پیشگویی
I'm trying to remove or replace them with an empty character, this code doesn't work and I can't replace them:
.replace(/("|')/g, "")
or
.replace(/["']/g, "")
How can I change this code to remove the single quotes?
How about this solution? Hope it helps!
console.log("فال ورق ' تاروت ' پیشگویی".replace(/'/g, ""));
console.log("code'123".replace(/'/g, ""));

javascript replace escaped " to work with Json parse

This is the input of javacript function that arise error after Json.parse
I know double escape string is working with JSON.parse, but how to convert current string to that one... I have tried couple of things, but couln't make it work.
pVis = '[{"Name":"Ecz., - \" Europharma\" \" -\"","Id":"402872"}]';
console.log('before replace'+pVis);
pVis = pVis.replace(/\\/g,"\\\\");
//pVis = '[{"Name":"Ecz., - \\" Europharma\\" \\" -\\"","Id":"402872"}]';
console.log('after replace'+pVis);
pVisitedsJson = JSON.parse(pVis);
Since it's interpreting ' character as escaped, it's impossible for me catch and change it on javascript side. So, I changed from the source.

JavaScript Surround quotes around a single string in multi lines

My string is somewhat like this :
<rules>
<transTypeRuleList>
<transType type='stocks'>
<criteria result='401kContribution' priority='0'>
<field compare='contains' name='description'></field>
</criteria>
<criteria result='401kContribution' priority='1'>
<field compare='contains' name='description'></field>
</criteria>
</transType>
</transTypeRuleList>
</rules>
I need to take this and paste it in eclipse as a string , I know eclipse has an option to escape multi line strings and all, but that gives me the string as with "\n \r" Which I don't want .
My ideal string would be just the Double quotes and a + at the end of each line somewhat like this.
var res= "<rules>"+
"<acctTypeRuleList>"+
"<acctTypetype='stocks'>"+
"<criteriaresult='individual'priority='0'>"+
"<fieldcompare='contains'name='accountName'></field>"+
"</criteria>"+
"<criteriaresult='individual'priority='1'>"+
"<fieldcompare='contains'name='accountName'></field>"+
"</criteria>"+
"</acctType>"+
"</acctTypeRuleList>"+
"<transTypeRuleList>"+
"<transTypetype='stocks'>"+
"<criteriaresult='401kContribution'priority='0'>"+
"<fieldcompare='contains'name='description'></field>"+
"</criteria>"+
"<criteriaresult='401kContribution'priority='1'>"+
"<fieldcompare='contains'name='description'></field>"+
"</criteria>"+
"</transType>"+
"</transTypeRuleList>"+
"</rules>";
While preserving the indentation. So I am looking at regex.
So I guess finding ^(.*)$ and replacing with "$1" + should have done the job but it doesn't work .
Have a look at the fiddle. :
Link
Thanks in advance.
This seems to work:
http://regexr.com/38ps2
What I did
added the multiline m switch
removed the empty line in the text. Looks like regexr has problems with that.
I'd try with this:
'var res = "' + xml.replace(/\r?\n\s*/g, '" +\r\n\t"') + '";';
But remember that Javascript does allow multiline strings. Just put a backslash at the end of each line:
"<rules>\
<transTypeRuleList>\
...\
</rules>";

Regular expression for removing whitespaces

I have some text which looks like this -
" tushar is a good boy "
Using javascript I want to remove all the extra white spaces in a string.
The resultant string should have no multiple white spaces instead have only one. Moreover the starting and the end should not have any white spaces at all. So my final output should look like this -
"tushar is a good boy"
I am using the following code at the moment-
str.replace(/(\s\s\s*)/g, ' ')
This obviously fails because it doesn't take care of the white spaces in the beginning and end of the string.
This can be done in a single String#replace call:
var repl = str.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
// gives: "tushar is a good boy"
This works nicely:
function normalizeWS(s) {
s = s.match(/\S+/g);
return s ? s.join(' ') : '';
}
trims leading whitespace
trims trailing whitespace
normalizes tabs, newlines, and multiple spaces to a single regular space
Try this:
str.replace(/\s+/g, ' ').trim()
If you don't have trim add this.
Trim string in JavaScript?
Since everyone is complaining about .trim(), you can use the following:
str.replace(/\s+/g,' ' ).replace(/^\s/,'').replace(/\s$/,'');
JSFiddle
This regex may be useful to remove the whitespaces
/^\s+|\s+$/g
Try:
str.replace(/^\s+|\s+$/, '')
.replace(/\s+/, ' ');
try
var str = " tushar is a good boy ";
str = str.replace(/^\s+|\s+$/g,'').replace(/(\s\s\s*)/g, ' ');
first replace is delete leading and trailing spaces of a string.

Categories

Resources