Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-07-2011, 08:27 PM   PM User | #1
CRC
New to the CF scene

 
Join Date: Oct 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
CRC is an unknown quantity at this point
RegExp() and Variables

hello all

I am working on a keyword filter and am stuck on something that I am hoping someone has more experience with

my problem is that I am trying to pass a regular expression as a variable to the RegExp() function in javascript but it does not seem to be working

example below

Code:
var X = '^(?=.*?(';
var Y = 'dog))(?=.*?(cat))(?=.*?(mouse'; // this gets created dynamically
var Z = ')).*$';

var A = X + Y + Z;
// result is ^(?=.*?(dog))(?=.*?(cat))(?=.*?(mouse)).*$ and is a valid and tested expression

var B = new RegExp(A, 'i');
I want to pass B to a search() function but it does not seem to be accepted as a valid expression anymore

thanks for any help

Last edited by CRC; 10-07-2011 at 10:27 PM..
CRC is offline   Reply With Quote
Old 10-07-2011, 08:33 PM   PM User | #2
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
if you want to pass a regExp as a variable in javascript you would do so with /regexp/
example:
Code:
var B = /[0-9]/;
blaze4218 is offline   Reply With Quote
Old 10-07-2011, 08:35 PM   PM User | #3
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
or in your case:
Code:
var B = /^(?=.*?(dog))(?=.*?(cat))(?=.*?(mouse)).*$/
blaze4218 is offline   Reply With Quote
Old 10-07-2011, 08:40 PM   PM User | #4
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
I would expect the following to work, but I don't know what your regExp catches, so I don't know how to test it
Code:
var X = /^(?=.*?(/;
var Y = /dog))(?=.*?(cat))(?=.*?(mouse/; 
var Z = /)).*$/;

var A = X + Y + Z;

var B = new RegExp(A, 'i');
blaze4218 is offline   Reply With Quote
Old 10-07-2011, 08:51 PM   PM User | #5
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
I found a way to test, I used the following code (it's a moderation of your original attempt, but with a regExp that I have verified) and it worked just fine.

Code:
<script>
var X = '[0-9]|';
var Y = '[a-z]'; 

var A = X + Y;

var B = new RegExp(A, 'g');
</script>
<span onclick="this.innerHTML=this.innerHTML.replace(B,'')">lshsdj--LJKLK--87678</span>
maybe you should've specified global and case insensitive, not just the latter?
blaze4218 is offline   Reply With Quote
Users who have thanked blaze4218 for this post:
CRC (10-07-2011)
Old 10-07-2011, 09:44 PM   PM User | #6
CRC
New to the CF scene

 
Join Date: Oct 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
CRC is an unknown quantity at this point
thanks !!

I think you helped me figure out that my real problem is further down the particular filtering script that I am working on and not necessarily the construction of the regex

after I research that a little there may be another thread on the way
CRC is offline   Reply With Quote
Reply

Bookmarks

Tags
expression, filter, regex, regexp, regular

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:41 AM.


Advertisement
Log in to turn off these ads.