Javascript RegExp not working in IE

The return_value parameter contains

   <textarea>{"id":43,"description":"","item_id":28,"callback":"addNewAttachment","filename":"foo.jpg",,"type":"posts","ext":"jpg","size":145}</textarea>

The following code removes the textarea tags in Firefox, Chrome, so you can access the content in arr [1]. In IE, an alert 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);
}

return_value comes from an ajax call. I am using jQuery.

TEST

This also does not work:

var re = new RegExp (/<textarea>(.+)<\/textarea>/);

Decision

The problem was that IE was getting textarea String in upper case and firefox in lower case.

The following regular expression solves it.

var re = new RegExp ('<textarea>(.+)</textarea)>','i');
+3
source share
3 answers

Is this a case sensitive question? new RegExp(..., "i")may I help?

+4
source

Try using a regex literal:

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

IE ? IE 7 :

<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>

Change . I checked this code in IE7 and it also works. test.xml is a file that contains a string and is located in a folder next to the HTML page using a script. I assume that it should also work with a dynamic page that returns the same.

<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>
0
source

Source: https://habr.com/ru/post/1714480/


All Articles