I have the following regex that determines (I hope) if a string is a URL or not.
/^(((\/[\w-]+)+\/?)|(((http|ftp|https):\/\/)[\w-]+))((\.[\w-]+)?([\w.,@?^=%&lL\/~+#-]*[\w@?~=%&:\/~+#-])?)$/gi
What I find is that if I save that regex to an object, then test the same string twice, I get two different results as shown in this JSFiddle
https://jsfiddle.net/gpc5trsw/
The code in this fiddle looks like this:
var test = /^(((\/[\w-]+)+\/?)|(((http|ftp|https):\/\/)[\w-]+))((\.[\w-]+)?([\w.,@?^=%&lL\/~+#-]*[\w@?~=%&:\/~+#-])?)$/gi
var str = "/api/WebTest";
document.getElementById("firstCtr").innerHTML = /^(((\/[\w-]+)+\/?)|(((http|ftp|https):\/\/)[\w-]+))((\.[\w-]+)?([\w.,@?^=%&lL\/~+#-]*[\w@?~=%&:\/~+#-])?)$/gi.test(str)
document.getElementById("secondCtr").innerHTML = /^(((\/[\w-]+)+\/?)|(((http|ftp|https):\/\/)[\w-]+))((\.[\w-]+)?([\w.,@?^=%&lL\/~+#-]*[\w@?~=%&:\/~+#-])?)$/gi.test(str)
document.getElementById("first").innerHTML = test.test(str)
document.getElementById("second").innerHTML = test.test(str)
With the following HTML:
<div>
First Run Control: <span id="firstCtr"></span>
</div>
<div>
Second Run Control: <span id="secondCtr"></span>
</div>
<hr/>
<div>
First Run: <span id="first"></span>
</div>
<div>
Second Run: <span id="second"></span>
The resulting HTML output is as such:
First Run Control: true
Second Run Control: true
First Run: true
Second Run: false
Why does the results of the test change the second time it's tested on the object version? JavaScript can be a cruel mistress sometimes...