Page 1 of 1
[SOLVED] Basic JavaScript RegExp
Posted: Wed May 18, 2005 12:05 pm
by anjanesh
After working so long with preg_match I thought I could get through this but... !
This one is JavaScript one though.
Code: Select all
<a href="e;some.php"e;>Click
Here</a>
<script type="e;text/javascript"e;>
var re = new RegExp("e;<a href=\"e;(.*?)\"e;>Click Here<\/a>"e;,"e;is"e;);
var result = re.exec(document.body.innerHTML);
alert(resultї1]);
</script>
Whats confusing is there is no alert() msg box AND there no msg in the FireFox's JavaScript Console. If there was some msg in the JavaScript Console I could have done something.
Anyone know what am I doing wrong here ?
Thanks
Posted: Wed May 18, 2005 1:21 pm
by Chris Corbyn
Well Javascript's regex implementation is pretty poor unfortunately.
There is no such "s" modifier in JS (we use "g" instead), also... make sure you put the <script> tags with this code after the body or it'll be undefined.
One last point. With the JS exec() method, if you want a preg_match_all() equivalent you need to use a while loop.
Code: Select all
<html>
<head>
</head>
<body>
<a href="e;foobar.html"e;>Click Here</a>
<a href="e;foothing.php"e;>Click me</a>
<script type="e;text/javascript"e;>
<!--
re = new RegExp("e;<a href=\"e;(.*?)\"e;>.*?<\/a>"e;,"e;ig"e;);
while(result = re.exec(document.body.innerHTML)) {
alert(resultї1]);
}
// -->
</script>
</body>
</html>
Posted: Wed May 18, 2005 1:34 pm
by anjanesh
I used the g modifier but returns null - I was hoping this would work because getElementsByTags("a") is returning the correct value.
Code: Select all
<html>
<head></head>
<body>
<a href="e;some.php"e;>Click
Here</a>
<script type="e;text/javascript"e;>
var re = new RegExp("e;<a href=\"e;(.*?)\"e;>Click Here<\/a>"e;,"e;ig"e;);
var result = re.exec(document.body.innerHTML);
alert(result);
alert(document.getElementsByTagName("e;a"e;)ї0].innerHTML);
</script>
</body>
</html>
There is no such "s" modifier in JS
Thats strange. I was referring to
this and according to it g,i,s,m,x modifiers exist.
Posted: Wed May 18, 2005 1:42 pm
by Chris Corbyn
Check you're Firefox (or whatever ou have) JavaScript console...
Error: invalid regular expression flag s
You can get around it using the | for alternation (i.e. (.|\s) ).

Posted: Wed May 18, 2005 1:49 pm
by anjanesh
Thanks for your reply.
I've decided to use RegExp - if not found call a function that traverses the entire <a> tags and return.
Posted: Thu May 19, 2005 9:48 am
by n00b Saibot
n00b Saibot to rescue
Code: Select all
<a href="e;some1.php"e;>Click Here</a>
<a href="e;some2.php"e;>Click Here</a>
<a href="e;some3.php"e;>Click Here</a>
<a href="e;some4.php"e;>Click Here</a>
<a href="e;some5.php"e;>Click Here</a>
<script>
var re = /<a href=\"e;(.*?)\"e;>Click Here<\/a>/gi;
var re1 = /href=\"e;(.*?)\"e;/i;
var result = document.body.innerHTML.match(re);
for (x=0; x< result.length; x++)
{
ext = re1.exec(resultїx]);
alert("e;Match #"e; + (x+1) + "e;\r\n"e; + resultїx] + "e;\r\nExtracted String is : "e; + extї1]);
}
</script>
I had to declare a 2nd regex because the 'g' parameter of the original regex kept failing alternatively when i exec'd it in the loop.
Posted: Thu May 19, 2005 10:29 am
by anjanesh
My code worked for single lines - I was looking for the 's' modifier equivalent in JS.
It doesnt catch :
Code: Select all
<a href="e;some.php"e;>Click
Here</a>