[SOLVED] Basic JavaScript RegExp

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] Basic JavaScript RegExp

Post 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=&quote;some.php&quote;>Click 
Here</a>
<script type=&quote;text/javascript&quote;>
var re = new RegExp(&quote;<a href=\&quote;(.*?)\&quote;>Click Here<\/a>&quote;,&quote;is&quote;);
var result = re.exec(document.body.innerHTML);
alert(result&#1111;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
Last edited by anjanesh on Wed May 18, 2005 1:49 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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=&quote;foobar.html&quote;>Click Here</a>
<a href=&quote;foothing.php&quote;>Click me</a>
<script type=&quote;text/javascript&quote;>
<!-- 
re = new RegExp(&quote;<a href=\&quote;(.*?)\&quote;>.*?<\/a>&quote;,&quote;ig&quote;);
while(result = re.exec(document.body.innerHTML)) {
	alert(result&#1111;1]);
}
// -->
</script>
</body>
</html>
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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=&quote;some.php&quote;>Click
Here</a>
<script type=&quote;text/javascript&quote;>
var re = new RegExp(&quote;<a href=\&quote;(.*?)\&quote;>Click Here<\/a>&quote;,&quote;ig&quote;);
var result = re.exec(document.body.innerHTML);
alert(result);
alert(document.getElementsByTagName(&quote;a&quote;)&#1111;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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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) ).

:wink:
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

n00b Saibot to rescue :lol:

Code: Select all

<a href=&quote;some1.php&quote;>Click Here</a>
<a href=&quote;some2.php&quote;>Click Here</a>
<a href=&quote;some3.php&quote;>Click Here</a>
<a href=&quote;some4.php&quote;>Click Here</a>
<a href=&quote;some5.php&quote;>Click Here</a>
<script>
var re  = /<a href=\&quote;(.*?)\&quote;>Click Here<\/a>/gi;
var re1 = /href=\&quote;(.*?)\&quote;/i;
var result = document.body.innerHTML.match(re);
for (x=0; x< result.length; x++)
{
ext = re1.exec(result&#1111;x]);
alert(&quote;Match #&quote; + (x+1) + &quote;\r\n&quote; + result&#1111;x] + &quote;\r\nExtracted String is : &quote; + ext&#1111;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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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=&quote;some.php&quote;>Click
Here</a>
Post Reply