Need to escape " , ' along with other UNICODE characters - javascript

I have the following string :
"ѯѰѱѲ & <b>BOLD</b> <>?:"{}|+_)(*&^%\\\\\\$##!~`,./;'[]\
(with the starting " and the " in the middle).
I tried the following 3 functions : escape, encodeURI and encodeURIComponents.
However all three give the error of : SyntaxError: Unexpected token { obviously due to the presence of starting " closing with the middle ".
I do not want to use regex replace to escape " and ' in the string. Is there any direct JS/jQuery function which would do the same?
CODE:
Was earlier doing this : e.title.toLowerCase().indexOf(encodeURIComponent($scope.nameToSearch.toLowerCase()))

or i could have it hardcoded too
For the hardcoded version, just put it in quotes with escapes for the quotes and backslashes:
// Quotes v v
var str = '"ѯѰѱѲ & <b>BOLD</b> <>?:"{}|+_)(*&^%\\\\\\\\\\\\$##!~`,./;\'[]\\';
// Escapes -------------------------------------^-^-^-^-^-^-----------^---^

Related

How to fix 'missing) after argument list' error in Javascript

Why is this giving me this error?
<script>
function loadImg() {
var imageChosen = document.getElementById('imageChosen').value.replace("C:\fakepath\","");
alert(imageChosen);
document.getElementById('image').src = imageChosen;
}
</script>
I expect the image with id "image" to show the chosen image.
The value in your call to replace() is not escaped properly.
The value should instead be:
"C:\\fakepath\\",""
Read more about escaping strings here
The problem is due to the escape string character \ (backslash)
When using strings in Javascript we may escape some character in the string. For example a break line (\n) or even a "(double quotes) when declaring the string or even an backslash \ need a escape.
Examples:
x = "my \\" // Will output as the same as "my \"
z = "my \"quotes\" // Will output as 'my "quotes" '

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>'));");

replacing a unicode character

I can't figure out how to remove a Unicode character using .replace(...) here's what I've tried
$(elem).click(function () {
var display = $("." + target).css('display');
var lastChar = display == 'none' ? 'a' : '8';
$("." + target).slideToggle(500);
alert('index: ' + $(this).text().indexOf('⇈'));
$(this).html($(this).html().replace('⇈','').replace('⇊','') + ' &#x21c' + lastChar + ';');
});
It is adding my double arrow up and down, but indexOf is always -1, and my replace calls are not removing the Unicode character. I'm looking at this now and thinking that if I start off with one of these Unicode chars I could just replace it with the other one... If I could get replace to work at all ;-)
What am I doing wrong? Thanks!
HTML entities get converted to actual unicode characters. Example:
document.body.innerHTML = "⇈";
console.log(document.body.innerHTML === "\u21c8"); // true
// Instead of a unicode esape sequence, you can write the
// actual unicode character. This is safe as long as you
// specify the correct encoding for your JavaScript files:
console.log(document.body.innerHTML === "⇈"); // true
So when you read the innerHTML via $(this).html() or the textContent via $(this).text(), you need to look for the actual unicode character given by its unicode escape sequence "\u21c8" or directly "⇈" and not its entity "⇈".
Try using String.fromCharCode()
The static String.fromCharCode() method returns a string created by
using the specified sequence of Unicode values.
Syntax
String.fromCharCode(num1[, ...[, numN]]);
Examples
myString.replace(String.fromCharCode(8648),''); for ⇈
myString.replace(String.fromCharCode(8650),''); for ⇊
myString.indexOf(String.fromCharCode(84,69,83,84);

Replacing " with \" in ASP

As the title suggests, I am trying to replacing a " with the escape character \". The reason I am doing this is because there is a quote in my string that represents inches and this string needs to be run through javascript code.
However, when I run my string in the javascript code, it only reads up to the inches character and forgets the rest of the string.
Any and all help is greaty appreciated.
A function like this will escape \, " and ':
function AddSlashes(str)
AddSlashes = replace(str,"\","\\")
AddSlashes = replace(AddSlashes,"'","\'")
AddSlashes = replace(AddSlashes,chr(34),"\" & chr(34))
end function
Source
If you define like this var s = 'some string';, the quote is work ok, but if you do like var s = "some string";, you should replace the quote to \", so the code is var s = "some string \ " with the quote";
also you need to replace the Vbcrlf and vbcr and vblf before the JS code

JavaScript Replace - u0009 .... with .replace(/\u0009/g,'');

I'd like to use Javascript to replace all instances of \u009 in a string
This doesn't seem to be working: .replace(/\u0009/g,'');
Do I need to escape something?
First, the question says "replace all instances of \u009 in a string".
But, the regex has replace(/\u0009/g,''); Is this a typo (different number of zeroes)?
Anyway, if the string only contains, unicode, horizontal tab characters (just one char), then the regex is fine.
If it actually contains 6 ascii chars, then the regex needs to be escaped, like so:
var oneChar = 'Pre \u0009 post';
var sixChars = 'Pre \\u0009 post';
//-- NOTE: If not using Firebug, replace 'console.log()' with 'alert()'.
console.log (oneChar + ' becomes --> ' + oneChar.replace (/\u0009/g, "") );
console.log (sixChars + ' becomes --> ' + sixChars.replace (/\\u0009/g, "") );
You need another escape .replace(/\\u009/g,''); :)

Categories

Resources