最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript RegExp does not work in IE - Stack Overflow

programmeradmin3浏览0评论

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

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 es 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');
Share Improve this question edited Aug 6, 2009 at 19:45 Andrea Ambu 39.6k14 gold badges56 silver badges77 bronze badges asked Aug 6, 2009 at 8:25 Sergio del AmoSergio del Amo 78.2k68 gold badges153 silver badges180 bronze badges 4
  • did u try 'var re = new RegExp (/<textarea>(.+)</textarea>/);' – palindrom Commented Aug 6, 2009 at 8:30
  • You say Variable *return_value* contains but you have re.exec(returned_value); in the code. Is this a typo in the question text or your code? – Helen Commented Aug 6, 2009 at 8:33
  • @palindrom: If you use /.../ it's already a RegExp object, you shouldn't create a new RegExp object from it... – Guffa Commented Aug 6, 2009 at 8:36
  • To make it more readable you can also use the case insensitive flag as other mentioned: new RegExp(..., 'i') – rslite Commented Aug 6, 2009 at 9:50
Add a ment  | 

3 Answers 3

Reset to default 4

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>
发布评论

评论列表(0)

  1. 暂无评论