This question already has answers here:
What are valid values for the id attribute in HTML?
(26 answers)
Need to escape a special character in a jQuery selector string
(7 answers)
Closed 2 years ago.
Edit:
I think the issue is not with concatenation itself but the special character in the id of the element I am trying to select.
Original:
I am having problem with concatenating two strings. It seems the problem is because one of the strings contains '!'. Sadly, I have no control over what that string can contain. I tried using '+' to make single string of the two before I tried concat(). But the error remains the same.
The relevant code is $('#id_online_status_'.concat(msg.id)).removeClass('text-success').addClass('text-muted');
Error is this:
Uncaught Error: Syntax error, unrecognized expression: #id_online_status_specific.YqzvRnpU!OPMxkuoFQILY
Please help with fixing this. I am not much familiar with JS in general.
change your code like this and try
$('#id_online_status_'+msg.id).removeClass('text-success').addClass('text-muted');
remove 'concat' and try with (+)
Related
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
It is the first time when I met some of the bad example of data format.
All date goes for me regarding to a region of a customer.
I cannot change it. I got string:
var str = "23.01.2019 23:05:58";
in order to get
var dt = new Date(str);
I need to execute at least
str.replace(/./g,'/');
But got a weird result. (tried "split" and "join") also nothing.
Thanks a lot for any help
The . in your regular expression means any character.
Try this instead...
str.replace(/\./g,'/');
By using \. you're "escaping" the dot and saying "this has to be a dot character"
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Match exact string
(3 answers)
Closed 5 years ago.
I have written the following function in javascript to validate a string that I will use as a file name. I would like to check for all the characters that are restricted by Windows OS as invalid while creating files. I checked the regular expression at RegExr, it seems to be working as expected but it doesn't work when called from an Angular controller and it only matches the first character in the parameter. I'm adding the file extension later on so that isn't a problem.
Can anybody help with it? I'm relatively new to regular expressions and would appreciate any help or pointers to useful resources.
function validateInput(value) {
if (!AngularUtils.isUndefinedOrNull(value)) {
var regex = new RegExp("[^<>/\\\\=|:*?\"]+$");
var regexOutput = regex.test(value);
if (!regex.test(value))
return true;
}
return false;
}
Edit:
Even after changing the regex to handle javascript constructors, I'm still getting valid matches for the following input: "sample_css", "sample=css","=sample"
Only the first string should be valid. jsfiddle here.
This question already has answers here:
JSON Javascript escape
(4 answers)
Closed 7 years ago.
I want to convert a json string to object by eval, but it fails with error like:
Uncaught SyntaxError: Unexpected identifier VM250:1
below is my string:
'[{"quiz_id":"3","_id":"1","option_in_json":"[{\"option\":\"1\",\"is_answer\":false},{\"option\":\"2\",\"is_answer\":true}]","question":"1+1"}]';
Seems there is something wrong in the bold part, but i don't know how to fix it
The code below is not working
var m='[{"quiz_id":"3","_id":"1","option_in_json":"[{\"option\":\"1\",\"is_answer\":false},{\"option\":\"2\",\"is_answer\":true}]","question":"1+1"}]';
eval(m);
The code below is working so i think the data structure of this json string is ok
var m=[{"quiz_id":"3","_id":"1","option_in_json":"[{\"option\":\"1\",\"is_answer\":false},{\"option\":\"2\",\"is_answer\":true}]","question":"1+1"}];
alert(m[0].option_in_json);
Also tried with $.parseJSON with no luck
It does not work because you are not escaping the data inside the string literal correctly. Look at the value of m in the first case, especially the quotation marks:
[{"option_in_json":"[{"option":"1","is_answer":false}]","question":"1+1"}]
// ^ ^
I removed some irrelevant data. You should be able to see that this cannot be valid JavaScript (or JSON), because the quotation mark before option terminates the string.
In order to put the data inside a string literal, you should either fix the data so that it doesn't contain nested JSON, or escape \:
'[{"option_in_json":"[{\\"option\\": ... }]"}]'
Better of course if you are not putting it in a string literal in the first place.
var m='[{"quiz_id":"3","_id":"1","option_in_json": [{"option":"1","is_answer":false},{"option":"2","is_answer":true}],"question":"1+1"}]';
// ^-- don't wrap in "" so no need to escape inner double quotes.
console.dir(JSON.parse(m));
This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 4 years ago.
Even if I use the m flag, javascript regex seems to isolate regex matching by lines.
Example:
"if\nend".match(/if(.*?)end/m)
=> null
I want this to match. How do I get around this?
You actually want s (a.k.a. "dotall"), not m, but javascript doesn't support that. A workaround:
"if\nend".match(/if([\s\S]*?)end/)
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 3 months ago.
I am unable to retrieve a value from a json object when the string has a dash character:
{
"profile-id":1234, "user_id":6789
}
If I try to reference the parsed jsonObj.profile-id it returns ReferenceError: "id" is not defined but jsonObj.user_id will return 6789
I don't have a way to modify the values being returned by the external api call and trying to parse the returned string in order to remove dashes will ruin urls, etc., that are passed as well. Help?
jsonObj.profile-id is a subtraction expression (i.e. jsonObj.profile - id).
To access a key that contains characters that cannot appear in an identifier, use brackets:
jsonObj["profile-id"]
In addition to this answer, note that in Node.js if you access JSON with the array syntax [] all nested JSON keys should follow that syntax
This is the wrong way
json.first.second.third['comment']
and will will give you the 'undefined' error.
This is the correct way
json['first']['second']['third']['comment']
For ansible, and using hyphen, this worked for me:
- name: free-ud-ssd-space-in-percent
debug:
var: clusterInfo.json.content["free-ud-ssd-space-in-percent"]
For anyone trying to apply the accepted solution to HomeAssistant value templates, you must use single quotes if you are nesting in doubles:
value_template: "{{ value_json['internet-computer'].usd }}"
If you are in Linux, try using the following template to print JSON value which contains dashes '-'
jq '.["value-with-dash"]'
It worked for me.