Javascript在Internet Explorer中的RegEx问题 [英] Javascript RegEx trouble in Internet Explorer
问题描述
我正在尝试使用正则表达式在Javascript中拆分字符串。我的代码如下:
I am trying to split a string in Javascript using a Regular Expression. My code is as follows:
var status = "This is a test http://yfrog.com/5y6eruj";
var regexp = /(http:\/\/yfrog\.com(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
matchParts = status.split(regexp);
alert(matchParts);
当我提醒 matchParts
时,在Chrome和FF中它是一个包含文本然后是url的数组。然而在IE中, matchParts
只是文本而网址已经消失了!
In Chrome and FF when I alert matchParts
it is an array containing the text and then the url. In IE however matchParts
is just the text and the url has vanished!
IE中有一个奇怪的错误或者我的正则表达式不太正确...请帮助!
Either there is an odd bug in IE or my Regular Expression isn't quite right...please help!
推荐答案
我不会为此使用split,因为它有错误: http://blog.stevenlevithan.com/archives/cross-browser-split
I would not use "split" for this, because it's buggy: http://blog.stevenlevithan.com/archives/cross-browser-split
尝试
var matchParts = regexp.exec(status);
。您可能需要修改一下正则表达式(我会尝试并更新)。
instead. You may have to tinker with the regex a bit (I'll try it and update).
编辑如果添加(。*)
到正则表达式的开头,你也会拿起前导文本。
edit If you add (.*)
to the beginning of the regular expression, you'll pick up the leading text too.
这篇关于Javascript在Internet Explorer中的RegEx问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!