[SOLVED] JavaScript: VAR inside a reg_exp

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

[SOLVED] JavaScript: VAR inside a reg_exp

Post by Calimero »

This is the situation:

Code is on the page that collects input from the FORM

Code: Select all

Function abc ()
{
i_vars = new Array ("<? echo $var1 ?>", "<? echo $var1 ?>", "<? echo $var1 ?>");

for ( var i = 0; i < i_vars.length; i++)
&#123; var pattern = "/i_vars&#1111;i]/gi";
  alert (i_vars&#1111;i]); // just to check taht for loop works, it works fine - each ford inside is separated

var second = first.replace (pattern, '<b>'+i_vars+'</b>');
.....
unimportant code...
---------------------------------------------------------

The problem I can't figure out is - How to insert a variable (i_vars) inside the pattern variable inside the reg_exp. I can't get JavaScript to pull the variable from the array above.


Thanks Ahead !
Last edited by Calimero on Mon Oct 04, 2004 4:20 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

javascript doesn't do variables inside strings like php does.

Code: Select all

Function abc (first)
&#123;
var i_vars = new Array ("<? echo $var1 ?>", "<? echo $var1 ?>", "<? echo $var1 ?>");
var pattern, second;

for ( var i = 0; i < i_vars.length; i++)
&#123;
  pattern = "/" + i_vars&#1111;i] + "/gi";
  alert(i_vars&#1111;i]);
  second = first.replace(pattern, '<b>' + ivars&#1111;i] + '</b>');

......
that's untested, as I think you need to use a RegExp object in order to have a variable regularexpression in Javascript. However, you could engineer the regular expression string to not need it in the first place.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

Nope,
with this code no errors are reported, but nothing is highlighted.

:cry:

RegExp object, ha ?!?

Do I search the docs, under these Keywords.
And if you have some sample code - just to see in example how it works.

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rough example

Code: Select all

<?php $vars = array('word1','wordfoo','word3');
array_walk($vars, 'preg_quote', '/');
?>
function foo(contents)
{
  return first.replace(/(<?php echo implode('|',$vars); ?>)/gi,"<b>\$1</b>");
}
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

Got the little basta...

Code: Select all

Function abc () 
&#123; 
i_vars = new Array ("<? echo $var1 ?>", "<? echo $var1 ?>", "<? echo $var1 ?>"); 

for ( var i = 0; i < i_vars.length; i++) 
&#123; 
if (i_vars&#1111;i] != "")

&#123;
var pattern = new RegExp (i_vars&#1111;i], "gi");

var present = document.getElementById('search_zone').innerHTML;
if
(document.getElementById && document.getElementById('search_zone') && document.getElementById('zone').innerHTML)
&#123;
var desired = present.replace (pattern, '<b>'+i_vars&#1111;i]+'</b>'); 
document.getElementById('search_zone').innerHTML = desired;
&#125;
&#125;
&#125;
&#125;
// The End (This time Happy  :)  )
NOTES:

Because of a bug it has with form elements, use <p id='search_zone'> </p> tags to limit the area you want to search for the pattern
Oh yeah, pray that your visitors do not enter single to 3 letters that match <html> tags inside your source - or it gets.... interesting !

Thanks for the tip Feyd.

2 out of 15 tasks for my Website v2 finished.

See ya :) .
Post Reply