Trying to print a unicode code point table - javascript

I am trying to make a unicode code points table that prints the code points till U+300
I change the number into hexadecimal and concatenate it with the unicode escape sequence.
When I try to concatenate the hexadecimal number with '\u' I get an error SyntaxError: Invalid Unicode Escape Sequence
Here's the code
How can I fix that error?

Change the print statement to print(num + ' => ' + String.fromCharCode("0x" + num));

Instead of this:
print(num + ' => ' + '\u' + num);
use this:
print(num + ' => ' + '\\u' + num);
Or, more concisely,
print(num + ' => \\u' + num);
You need to escape the \ itself to include it in a string literal.

Related

JavaScript's console.log printing unwanted characters

So basically what I'm trying to do is print a simple string to the screen using the console.log function.
Here's an example :
const fromLabel: string = '["' + "AppExp" + '"]' + '\n' + '["' + "AppExp" + '"]';
And I ultimately wanna print it, so I go:
console.log(fromLabel);
and my output is:
[\"AppExp\"]\n[\"AppExp\"]
So, basically no carriage return and unwanted '\'.
Any idea what could be the problem?
EDIT: Never mind. I was working with objects and to print them I used JSON.stringify.. little did I know I used it on this string as well ..my bad
Backslashes are escaping certain characters in the string. Your string is put together in a weird way—you're mixing "" and ''. Try this:
var str = '["' + 'AppExp' + '"]' + '\n' + '["' + 'AppExp' + '"]'
console.log(str)
try this code with template literals
I omitted the : string to be able to run the snippet but remember to add it!
const fromLabel = `[""AppExp""]
[""AppExp""]`;
console.log(fromLabel);
or in case you do not want duplicate " chars
const fromLabel: string = `["AppExp"]
["AppExp"]`;
I hope it helps! :)

regular expression to check only one decimal point

I have following regular expression to check only one decimal point for type number tag in html
^-?[0-9]*\\.?[0-9]*$
but this regular failed to check If I put decimal at the end e.g 12.12.
what further I have to add to check this
I think your regex can be easily fixed using a + instead of last * quantifier:
^-?[0-9]*\.?[0-9]+$
Tests:
const regex = /^-?[0-9]*\.?[0-9]+$/gm;
console.log('regex.test?')
console.log('12 = ' + regex.test('12'));
console.log('12. = ' + regex.test('12.'));
console.log('12.1 = ' + regex.test('12.1'));
console.log('12.12. = ' + regex.test('12.12.'));
console.log('-1 = ' + regex.test('-1'));
console.log('-1. = ' + regex.test('-1.'));
console.log('-1.2 = ' + regex.test('-1.2'));
console.log('-.12 = ' + regex.test('-.12'));
console.log('-. = ' + regex.test('-.'));
console.log('-. = ' + regex.test('-'));
console.log('. = ' + regex.test('.'));
Demo
Can you try the below : [1-9]\d*(\.\d+)?$
The simplest way to allow a possible . at the end is to have \.? just before the $. Also, the double \ looks wrong (unless you need it for escaping a \ in the context in which you are using it):
^-?[0-9]*\.?[0-9]*\.?$
But please recognize that your regex does not require any actual digits, so will match some non-numbers, like ., -. and (with my edit) -.. The above regex will also match an empty string!
You will want to either change your regex to require digits, or take into account somewhere else that they might not be there.

Replacing string in multiline variable not working [duplicate]

This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Closed 5 years ago.
I am trying to replace the string {NUM} in the following variable:
var table = ' <table class="full-width">\r\n' +
' <tbody>\r\n' +
' <tr>\r\n' +
' <td width="75%" class="border-right-dotted left-line-tab">\r\n' +
' <span id="47_TOTAL-CHARGES_D_{NUM}" class="input-text"></span>\r\n' +
' </td>\r\n' +
' <td width="25%" class="center-text">\r\n' +
' <span id="47_TOTAL-CHARGES_C_{NUM}" class="input-text"></span>\r\n' +
' </td>\r\n' +
' </tr>\r\n' +
' </tbody>\r\n' +
' </table>\r\n';
using the following jquery replace:
table = table.replace("/{NUM}/gm", num);
but it doesn't seem to work. Testing the regex in https://regex101.com/ seems to present that the regex is fine, but it still does not replace the text as expected.
Just remove the double quotes:
table = table.replace(/{NUM}/gm, num);
A text within double quotes defines a string literal whilst you need a regular expression literal, which should be surrounded by slashes.

How to apply regex in javascript to obtain single quote from json

sample:
"{ ' foo ' : ' bar ' baz ' bob ' , 'foo2':'Barr ' s' }"
I want single quotes to be filtered, so that i can escape ' with \' and process.
You can use a simple str.replace() on that string to escape as follows:
"{ ' foo ' : ' bar ' baz ' bob ' , 'foo2':'Barr ' s' }".replace("'", "\'");
However, as other have stated, your json string is formatted wrong and the solution won't overall because it's only the nested single quote in 'Barr ' s' value that you want to escape. Note: regex won't be able to solve this because it can't match open closing grammar rules or the inverse.

JSON parse string having Apostrophe (Single Quote)

How do i parse the following string
var a = JSON.parse('[' + '{"NoteName":"it's my life","UserId":"100","NoteActive":true,"UserEmail":"admin#dev.xrc.com","CreatedDate":"8/13/2012 1:47:35 PM"}' + ']');
You have just to escape a single quote it\'s
var a = JSON.parse('[' + '{"NoteName":"it\'s my life","UserId":"100","NoteActive":true,"UserEmail":"admin#dev.xrc.com","CreatedDate":"8/13/2012 1:47:35 PM"}' + ']');
console.log(a);
Replace it's with it\'s
'[' + '{"NoteName":"it\'s my life","UserId":"100","NoteActive":true,"UserEmail":"admin#dev.xrc.com","CreatedDate":"8/13/2012 1:47:35 PM"}' + ']'
You can escape (interpret solely as characters) quote marks using backslash.
"\"" or '\''

Categories