I have a string named uppercase and if it contains letters, nothing happens. Although, if it doesn't then it evaluates uppercase. Here's my if and else statement:
if(uppercase.substring(0,9) == "CALCULATE")
{
if(uppercase.substring(10,uppercase.length.contains("[a-zA-Z]+")))
{
} else{
var ans = eval(uppercase.substring(9,uppercase.length));
sendSpecialChat = [true, ans];
}
}
Although, it doesn't work. What's wrong with my code?
Likely your issue is here:
uppercase.substring(10,uppercase.length.contains("[a-zA-Z]+")
If you are testing if the part of uppercase contains any letters beyond the 10th character (allowing for zero index), then you can use substring with a suitable regular expression, e.g.:
/[a-z]/i.test(uppercase.substring(10,uppercase.length))
var uppercase = "0123456789Hey"
console.log('"' + uppercase + '" : ' +
/[a-z]/i.test(uppercase.substring(10,uppercase.length)));
var uppercase = "0123456789*&^"
console.log('"' + uppercase + '" : ' +
/[a-z]/i.test(uppercase.substring(10,uppercase.length)));
Or just a regular expression:
var uppercase = "0123456789Hey";
console.log('"' + uppercase + '" : ' + /^.{10}[a-z]/i.test(uppercase));
var uppercase = "0123456789*&^";
console.log('"' + uppercase + '" : ' + /^.{10}[a-z]/i.test(uppercase));
Related
I need to break a string apart after certain characters.
document.getElementById("result").innerHTML = Monster + "<p id='vault" + loop + "'> || HP: " + HP + "</p>" + " || Defense: " + Def + " || Attack: " + ATK + " || Can it Dodge/Block: " + DB + " || Can it retaliate: " + RET + " || Initative: " + INT + " || Exp: " + MEXP + " <input type='submit' class='new' onclick='Combat(" + loop + ")' value='FIGHT!'></input>" + "<br><br>" + A;
function Chest(id){
window.open('LootGen.html', '_blank');
}
function Combat(id){
document.getElementById("C").value = document.getElementById("vault" + id).innerHTML;
}
When this runs the value that results is:
|+HP:+20
However I only want '20' part,now keep in mind that this variable does change and so I need to use substrings to somehow pull that second number after the +. I've seen this done with:
var parameters = location.search.substring(1).split("&");
This doesn't work here for some reason as first of all the var is an innher html.
Could someone please point me in the write direction as I'm not very good at reading docs.
var text = "|+HP:+20";
// Break string into an array of strings and grab last element
var results = text.split('+').pop();
References:
split()
pop()
using a combination of substring and lastIndexOf will allow you to get the substring from the last spot of the occurrence of the "+".
Note the + 1 moves the index to exclude the "+" character. To include it you would need to remove the + 1
function Combat(id){
var vaultInner = document.getElementById("vault" + id).innerHTML;
document.getElementById("C").value = vaultInner.substring(vaultInner.lastIndexOf("+") + 1);
}
the code example using the split would give you an array of stuff separated by the plus
function Combat(id){
//splits into an array
var vaultInner = document.getElementById("vault" + id).innerHTML.split("+");
//returns last element
document.getElementById("C").value = vaultInner[vaultInner.length -1];
}
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.
I am working on VS2103 Cordova App. I have created list of items. I want to pass data to another page when i press on item. I've created this list by jQuery.
Here is my code :
for (var i = 0; i < data.length; i++) {
if ((Provider == "Doctors")) {
$("#list").append('<li class="list-message" ><a class="w-clearfix w-inline-block" href="javascript:ProviderDetails(' + data[i].DoctorName + ',' + data[i].DoctorAddress + ',' + data[i].DoctorPhone + ',' + data[i].DoctorPhone2 + ',' + data[i].DoctorPhone3 + ',' + data[i].DocLat + ',' + data[i].DocLong + ',' + data[i].DoctorNotes + ',' + data[i].Category + ');" data-load="1"><div class="w-clearfix column-left"><div class="image-message"><img src="images/Doctors.png"></div></div><div class="column-right"><div class="message-title">' + data[i].DoctorName + '</div><div class="message-text">' + data[i].DoctorAddress + '</div></div></a></li>');
}
}
And here is my function :
function ProviderDetails(Name, Address, Tel, Phone2, Phone3, Lat, Lang, Notes, Category) {
localStorage.setItem("Name", Name);
localStorage.setItem("Address", Address);
localStorage.setItem("Tel", Tel);
localStorage.setItem("Phone2", Phone2);
localStorage.setItem("Phone3", Phone3);
localStorage.setItem("Lat", Lat);
localStorage.setItem("Lang", Lang);
localStorage.setItem("Notes", Notes);
localStorage.setItem("Category", Category);
window.location.href = "../Details.html";
}
It doesn't do any thing when i press any items . Any help ?
Pay attention on how you build the string:
href="javascript:ProviderDetails(' + data[i].DoctorName + ',' ......
you need to add the string delimiters:
href="javascript:ProviderDetails(\'' + "data[i].DoctorName" + '\',\'' .....
Your function is declared as:
function ProviderDetails(Name, Address, Tel, Phone2, Phone3, Lat, Lang, Notes, Category)
{
....
}
Now, because your function expects strings as input you can call your function as:
ProviderDetails('string1', 'string2', .....)
Your javascript loop instead produces:
ProviderDetails(string1, string2, .....)
For javascript now the parameters are considered as variables, i.e., string1 is no more a string but a value contained in the variable string1.
But because you do not have such a variable your function call does not work.
So, the delimiters are important to instruct js to understand the beginning and end of a string.
As a delimiter you can you the symbols: ' or ".
But you need to escape the delimiter itself if you want to use it inside the strings:
var a = 'this isn't a string'; // wrong because the inner delimiter is not escaped.
var a = 'this isn\'t a string'; // OK because the inner delimiter is escaped
Of course if you use inside the string the other delimiter you do not need to escape it.
var a = "this isn't a string"; // this is OK
I have two Parse generated objectId strings that I know are equal, because I print them out and read them, and they are the same.
They are requestedUserId and requestingUserId.
I have tried as mentioned in the comments to check for invisible characters
console.log('"' + requestedUserId + '"')
console.log('"' + requestingUserId + '"')
However, as suspected, they print out equal.
The code below never runs, it jumps to the else statement. Is there a problem with my logic, or anything else that is readily apparent?
Parse.Cloud.beforeSave("FriendRequest", function(request, response) {
var requestedUserId = request.object.get("to")
var requestingUserId = request.object.get("from")
console.log('"' + requestedUserId + '"')
console.log('"' + requestingUserId + '"')
// One cannot request oneself
if (requestedUserId == requestingUserId) {
console.log("can't send a request to yourself")
response.error("can't send a request to yourself");
} else {
(...)
}
});
As per my comment, I suggest that you check the length of the 2 strings rather than relying on visibility in your console.
var str1 = 'abc123';
var str2 = 'abc123' + String.fromCharCode(0);
console.log('"' + str1 + '"', str1.length);
console.log('"' + str2 + '"', str2.length);
console.log(str1 == str2);
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