Sergio del Amo
Sergio del Amo

Reputation: 78106

Javascript RegExp does not work in IE

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');

Upvotes: 3

Views: 9217

Answers (3)

KooiInc
KooiInc

Reputation: 122906

Try using a regex literal:

var r = /<textarea>(.+)<\/textarea>/i;

Upvotes: 4

searlea
searlea

Reputation: 8378

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

Upvotes: 4

rslite
rslite

Reputation: 84703

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>

Upvotes: 0

Related Questions