Seeking some cheeky on the fly help here. Been staring at this for along time and cannot see why the whole thing is not maintaining being a string:
var str =
'<script type="cats/conversion">
{"type":"REGISTER",
"params":{"partner_conversion_id":"' + {{sku}} + '",
"f":"' + {{accountHolderName}} + '",
"e":"' + {{dl userid}} + '"}
}
<\/script>';
I had expected that all of str is a string but my text editor is telling me that it's not. Where have I typeod a ' or "?
Help!!
You can't (safely and reasonably) do multi-line string literals in JavaScript. You have to build them up using concatenation (the + operator). You can do it like this:
var str = '<script type="cats/conversion">' +
'{"type":"REGISTER",' +
'"params":{"partner_conversion_id":"' + {{sku}} + '",' +
'"f":"' + {{accountHolderName}} + '",' +
'"e":"' + {{dl userid}} + '"}' +
'}' +
'<\/script>';
Note: I have no idea what the {{sku}} and similar placeholders in your script are. I assume those are for some templating system. They are not, as written, valid syntax.
Edit: Technically, as Alex pointed out in his answer, you can do multi-line literals by escaping the newline with \. As Alex says, however, this is "considered horrible practice" and prone to breaking for all kinds of reasons. Don't do it!
Try this
var str = '<script type="cats/conversion">'+
'{"type":"REGISTER",'+
'"params":{"partner_conversion_id":"' + sku + '",'+
'"f":"' + accountHolderName + '",'+
'"e":"' + dl_userid + '"}'+
'} '+
'<\/script>';
In Javascript you have to add a \ to the end of a line to indicate a multi-line string. This is considered HORRIBLE practice, because if the \ is followed by any whitespace, it will throw a syntax error.
var ok = "this is \
my multiline string!";
var notOk = "this is \
my multiline string!";
The 2nd is an error because there is a space after the \ at the end of the line.
If you are using ES6, you can use template strings with backticks instead:
var x = `This is my
multiline string!`;
If ES6 is not an option and you don't want to follow bad practices by using \, concatenate multiple strings together or insert newlines in your string like so:
var myString = "this is line 1" +
" this is line 2" +
" this is line 3";
or
var myString = "this is line 1\n this is line 2\n this is line 3";
You either do it in one line
var str ='<script type="cats/conversion"> {"type":"REGISTER", "params":{"partner_conversion_id":"' + {{sku}} + '", "f":"' + {{accountHolderName}} + '", "e":"' + {{dl userid}} + '"}} <\/script>';
For readability if you want it to be multi lined you have to concatenate the string.
var str = '<script type="cats/conversion">' +
'{"type":"REGISTER",' +
'"params":{"partner_conversion_id":"' + {{sku}} + '",' +
'"f":"' + {{accountHolderName}} + '",' +
'"e":"' + {{dl userid}} + '"}' +
'}' +
'<\/script>';
I think you mean to do this:
var str =
'<script type="cats/conversion">
{"type":"REGISTER",
"params":{"partner_conversion_id":"' + '{{sku}}' + '",
"f":"' + '{{accountHolderName}}' + '",
"e":"' + '{{dl userid}}' + '"}
}
<\/script>';
The {{}}'s need to be wrapped in quotes.
Related
How to get regex with replace method? In my case I've got string which uses char / between.
input:
var string = "cn/" + companyName + "/st/" + state + "/ic/" + incCi + "/pr/" + priority + "/es/" + emplSystem + "/mc/" + mainCategory + "/sc/" + subCategory + "/ty/" + type;
output:
"cn/Nemesis Group/st/2/ic/null/pr/1 - High/es/null/mc/Add/Button/sc/Core/Label/ty/str"
variable mainCategory and subCategory returns string 'Add/Button' and 'Core/Label'
How to replace 'Add/Button' to 'Add%2FButton' and 'Core/Label' to 'Core%2FLabel' without changing any other char?
string.replace("\/", "%2F")
will change all char / to %2F
You can use encodeURIComponent() and decodeURIComponent() to transform this String
Example:
const companyName = "Company",
state = "State",
incCi = "IncCi",
priority = "Priority",
emplSystem = "EmplSystem",
mainCategory = 'Add/Button',
subCategory = 'Core/Label',
type = "Type";
var string = "cn/" + companyName + "/st/" + state + "/ic/" + incCi + "/pr/" + priority + "/es/" + emplSystem +
"/mc/" + encodeURIComponent(mainCategory) +
"/sc/" + encodeURIComponent(subCategory) + "/ty/" + type;
console.log(string)
It sounds to me like you are looking to encode the url. You can use encodeURI in JS to encode a url.
let encodedURL = encodeURI(url);
You can read more about it here.
If you want to encode the string altogether without ignoring any domain related parts, you can us encodeURIComponent()
let encodedURL = encodeURIComponent(url);
You can read more about their differences here.
EDIT:
If you are not encoding a url and you just want to repalce / with %2F only in mainCategory and subCategory then you need to run the regex on the string itself before joining them.
var string = "cn/" + companyName +
"/st/" + state +
"/ic/" + incCi +
"/pr/" + priority +
"/es/" + emplSystem +
"/mc/" + mainCategory.replace("\/", "%2F") +
"/sc/" + subCategory.replace("\/", "%2F") +
"/ty/" + type;
All the \n are removed from the string when the text is put inside mail body .
var valuess = Object.entries(feedBackText);
valuess.forEach(function (key) {
responseText = responseText.concat(' ' + key[0] + ':' + key[1] + '\n');
});
var parsedString = responseText.toString();
window.location = "mailto:myid#gmail.com"+"?subject="+subjectmail+"&body=" +
parsedString;
The following demonstrates how you can solve this using the built-in encodeURIComponent function:
var parsedString = "text on the" + "\n" + "next line";
var link = "mailto:myid#gmail.com" + "?subject=Example&body=" +
encodeURIComponent(parsedString);
console.log(link);
I am trying to make a dynamic HTML which I'll append using jquery before append if I'll console the HTML everything looks fine but after append in browser The whole structure messed up.
Here is my HTML :
<script>
var title = "my title";
var toolbar ='<hm_toolbar user="current_user " upp="hm" index="hmIndex "></hm><synapse_toolbar uuid=" hm.hm_pk " my-method="get_linked_facts" ng-if=" flag == true "></synapse_toolbar>';
var map_html = '<a onclick="panToMarker(' + lat + ',' + long + ')"> ' + title +'"</a>' + toolbar ;
var li_html = "$('#" + title + "').append('<li class=\"list-group-item\"><div dynamic=\" " + map_html + " \"></div></li> ')" ;
var g =title.replace(/ /g,"_");;
var fn = "function "+ g +"(){ console.log('--working--'); "+ li_html +"; }";
console.log(fn)
eval(fn);
}
</script>
When the above li_HTML does its work means append the HTML the appended is all messed up
Appended HTML :
<li class="list-group-item"><div pantomarker(49.711083,6.251445)"="" dynamic=" <a onclick="> Test"<hm_toolbar index="hmIndex " upp="hm" user="current_user "></hm_toolbar><synapse_toolbar ng-if=" flag == true " my-method="get_linked_facts" uuid=" hm.hm_pk "></synapse_toolbar> "></div></li>
I know I have messed up the concatenation using with quotes but I am not able to fixed the bug .
You said that the console.log() looks OK but you have eval() after that which means that you have to escape some quotes twice.
Try this:
var toolbar ='<hm_toolbar user=\\\'current_user \\\' upp=\\\'hm\\\' index=\\\'hmIndex \\\'></hm><synapse_toolbar uuid=\\\' hm.hm_pk \\\' my-method=\\\'get_linked_facts\\\' ng-if=\\\' flag == true \\\'></synapse_toolbar>';
var map_html = '<a onclick=\\\'panToMarker(' + lat + ',' + long + ')\\\'> ' + title +'</a>' + toolbar ;
I am trying to parse a formula, and display it on screen.
For example I should be able to take <path>T Q, where <path>T cannot change, and Q is a variable. It accepts it,however when printing it on screen again the only thing that will appear is T Q. I want <path>T Q to appear fully.
Other examples of accepted formulae are
(B & A)
~A
~(B&A)
<path>T (B & A)
etc
My code is something like this
var beginPartBUC = '^<path>\\(',
beginPart = '^\(',
unaryPart = '(?:~|<path>T)',
propOrBinaryPart = '(?:\\w+|\\(.*\\))',
subwffPart = unaryPart + '*' + propOrBinaryPart,
endPart = '\\)$';
// binary connective regexes
var conjRegEx = new RegExp(beginPart + '(' + subwffPart + ')&(' + subwffPart + ')' + endPart), // (p&q)
implRegEx = new RegExp(beginPart + '(' + subwffPart + ')->(' + subwffPart + ')' + endPart), // (p->q)
equiRegEx = new RegExp(beginPart + '(' + subwffPart + ')<->(' + subwffPart + ')' + endPart); // (p<->q)
// untilRegEx = new RegExp(beginPartBUC + '(' + subwffPart + ')U(' + subwffPart + ')' + endPart); //<path>(p U q))
As Barmar pointed out, you're writing to html and <path> resembles valid html. You can do this
currentFormula.html('<strong>Current formula:</strong><br>' + wff.ascii.replace(/>/g, ">").replace(/</g, "<"))
As an additional note, backticks are used on StackOverflow like this: `sample code` which produces sample code. This feature is available in comments as well.
Alternatively, in posts (not comments), you can indent each line with a tab or four spaces (easily done by pressing { } in the post editor.
I have to escape two special characters " and , in the string with the following rules.
Example:-
Mercu"ry should be converted into "Mercu""ry"
Mercu,ry should be converted into "Mercu,ry"
Mer"cu,ry should be converted into "Mer""cu,ry"
Rules:-
Meaning comma or double quote should be escaped with double quote.
Comma will escaped by wrapping the whole word in double quotes.
If Double quote is found, then it double quote should be added at its
position. Also the whole word should be wrapped inside the double
quotes.
Please suggest the regex pattern in javascript.
var test = [
'Mercu"ry', 'Mercu,ry', 'Mer"cu,ry', 'Mercury'
];
for (x in test) {
var s = test[x];
if (s.indexOf('"') != -1) {
s = s.replace(/"/g, '""');
}
if (s.match(/"|,/)) {
s = '"' + s + '"';
}
alert(s);
}
Test: http://jsfiddle.net/ZGFV5/
Try to run the code with Mer""cury :)
Just always wrap the word in double quotes, and replace all double quotes with two:
function escapeWord(word) {
return '"' + word.replace(/"/g, '""') + '"';
}
The regular expression to achieve this is /"/g, so the following will work for your examples:
var test1 = 'Mercu"ry'
var test2 = 'Mercu,ry'
var test3 = 'Mer"cu,ry'
var regex = /"/g;
var example1 = '"' + test1.replace(regex, '""') + '"';
var example2 = '"' + test2.replace(regex, '""') + '"';
var example3 = '"' + test3.replace(regex, '""') + '"';
alert(example1 + " : " + example2 + " : " + example3);
Example fiddle