使用/.../和RegEx与exec时的结果不同

时间:2016-01-08 19:46:57

标签: javascript regex

为什么会有所不同?

var addressValue = "http://partners.webmasterplan.com/click.asp?ref=755891&site=8883&type=text&tnb=2&diurl=http://www.redcoon.de/B440080-Tefal-FV-9640_B%C3%BCgeleisen";
var reAffilinet = new RegExp("diurl=[(http|https):\/\/]*w*\.*([a-z]*)");
reAffilinet.exec(addressValue); 
// ["diurl=http://www.redcoon.de/B440080-Tefal-FV-9640_B%C3%BCgeleisen", ""]

由此:

var addressValue = "http://partners.webmasterplan.com/click.asp?ref=755891&site=8883&type=text&tnb=2&diurl=http://www.redcoon.de/B440080-Tefal-FV-9640_B%C3%BCgeleisen";
var reAffilinet = /diurl=[(http|https):\/\/]*w*\.*([a-z]*)/;
reAffilinet.exec(addressValue); 
// ["diurl=http://www.redcoon.de/B440080-Tefal-FV-9640_B%C3%BCgeleisen", ""]

据我所知:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions他们应该是一样的......?

1 个答案:

答案 0 :(得分:2)

正则表达式文字的便利属性是您不必转义\

如果要使用RegExp构造函数,则必须进行此转义才能定义string literal

var reAffilinet = new RegExp("diurl=[(http|https):\\/\\/]*w*\\.*([a-z]*)");