Include single quotation in url string in Apps Script [duplicate] - javascript

This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 6 months ago.
What is the correct way to pass the single quotes inside the [ ] in this url please in my Apps Script?
https://{{domain}}/api/1.0.0/{apiKey}/appointments?where=['start,>=,2017-05-26T07:23:46-07:00','end,<=,2017-05-26T07:23:46-07:00']
I have:
var url = 'https://{{domain}}/api/1.0.0/' + {apiKey} + '/appointments?where=[' + ? + 'start,>=,2017-05-26T07:23:46-07:00' + ? +',' + ? + 'end,<=,2017-05-26T07:23:46-07:00' + ? + ']'

Why not simply use double quote ".
var url = "https://{{domain}}/api/1.0.0/" + {apiKey} + "/appointments?where=['start,>=,2017-05-26T07:23:46-07:00','end,<=,2017-05-26T07:23:46-07:00']";

Related

Javascript : replace HTML-Charakters with ASC2 Charakters [duplicate]

This question already has answers here:
Unescape HTML entities in JavaScript?
(33 answers)
Closed 19 days ago.
is there a special function for this replacement in Javascript ?
(replaceAll)
PARAMS+= "&Ueberschrift=" + ueberschrift.replaceAll(">",">").replaceAll("<","<");
PARAMS+= "&TextBaustein=" + textBaustein.replaceAll(">",">").replaceAll("<","<");
PARAMS+= "&Beurteilung=" + beurteilung.replaceAll(">",">").replaceAll("<","<");
edit: there is a replaceAll() method in JS, my bad !
anyhow, you can use the replace() method and use a regular expression to replace all occurrences of a string, taken from your provided example you could do something like this:
PARAMS += "&Ueberschrift=" + ueberschrift.replace(/\>/g, ">").replace(/\</g, "<"); PARAMS += "&TextBaustein=" + textBaustein.replace(/\>/g, ">").replace(/\</g, "<"); PARAMS += "&Beurteilung=" + beurteilung.replace(/\>/g, ">").replace(/\</g, "<");
To elaborate:
'g' flag indicates that the replacement should occur for all matches (not just the first one)
> and < characters are escaped to match the actual > and < characters.
'&gt' and '&lt' (HTML escape codes for '>' and '<')

is it possible to concat 2 regular expression strings in javascript [duplicate]

This question already has answers here:
Combining regular expressions in Javascript
(6 answers)
Closed 4 years ago.
I have 2 regex strings in javascript and I need to concat them into 1 regex. I saw somewhere this could be done using |
example:
passwordRegex:RegExp = '(?=.*[A-Za-z])(?=.*\\d)(?=.*[$#$!%*#?&^])[A-Za-z\\d$#$!%*#?&^]{8,}';
hasFourConsecutiveRegex:RegExp = '(.)\\1\\1\\1';
combinedRegex:RegExp = new RegExp(this.passwordRegex.source + ' | ' + this.hasFourConsecutiveRegex.source );
is this how its done?
You can do like this:
var passwordRegex = new RegExp('(?=.*[A-Za-z])(?=.*\\d)(?=.*[$#$!%*#?&^])[A-Za-z\\d$#$!%*#?&^]{8,}');
var hasFourConsecutiveRegex = new RegExp('(.)\\1\\1\\1');
var combinedRegex = new RegExp(passwordRegex.source + ' | ' + hasFourConsecutiveRegex.source );

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.

Why my string replace is not working in JavaScript? [duplicate]

This question already has answers here:
JS replace not working on string [duplicate]
(2 answers)
Closed 8 years ago.
I'm trying to replace a placeholder in a string I'm generating.
My string looks like this:
var s = 'module("SlapOS UI Basic Interaction"); ' +
'asyncTest( "${base_url}", function() { ' +
' expect( __number__ ); ' +
' ok(testForElement("div#global-panel"), "element present");' +
' start(); })';
And I want to replace __number__.
I can get the index correctly like so:
s.indexOf("__number__");
but replacing does not work...
s.replace("__number__", "1");
Question:
What am I doing wrong here? Makes no sense to my why it does not work.
The replace method does not modify the existing string. It returns a new one.
var result = s.replace("__number__", "1");

Can I do string formatting in Javascript? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript equivalent to printf/string.format
I am creating code like this:
var ds = "ds=" + encodeURIComponent($('#DataSource').val());
var ex = "ex=" + encodeURIComponent($('#EID').val());
var to = "to=" + encodeURIComponent($('#TID').val());
var st = "st=" + encodeURIComponent($('#SID').val());
window.location.href = '/person?' + ds + "&" + ex + "&" + to + "&" + st;
Is there some way in Javascript that I could use formatting to make the code look a bit cleaner? Also do I need to encode each element or can I just encode the variable abc?
There's not a lot you can do, really. You could alias the encodeURIComponent method and use an array with join() if you're looking for something a little neater:
var e = encodeURIComponent,
arr = [
"ds=" + e($('#DataSource').val()),
"ex=" + e($('#EID').val()),
"to=" + e($('#TID').val()),
"st=" + e($('#SID').val())
];
window.location.href = '/person?' + arr.join("&");
Use <form> and put your input tags in them and you can call $('formContainer').serialize();
Its better to use name-value pairs(query strings), while sending data to URI. provide names as id to all of the input tags that you wish to capture. You will get something like:
/person/DataSource=ds&EID=ex&TID=to&SID=st
-HTH
Why don't you just write:
window.location.href = encodeURIComponent('/person?' +
$('#DataSource').val() + "&"
+ $('#EID').val() + "&" + $('#TID').val() + "&"
+ $('#SID').val());
Take a look at sprintf()
Also this can help: JavaScript equivalent to printf/string.format
var str = "/person?ds=%x&ex=%x&to=%x&st=%x";
var tokens = [
$("#DataSource").val(),
$("#EID").val(),
$("#TID").val(),
$("#SID").val()
];
tokens.forEach(function (token) {
str = str.replace("%x", encodeURIComponent(token));
});
location.href = str;
This will absolutely fall over if you have %x in your token string.
If you want a more generic solution try underscore.string

Categories