Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    CRC
    CRC is offline
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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 11:27 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    if you want to pass a regExp as a variable in javascript you would do so with /regexp/
    example:
    Code:
    var B = /[0-9]/;

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    or in your case:
    Code:
    var B = /^(?=.*?(dog))(?=.*?(cat))(?=.*?(mouse)).*$/

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    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');

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    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?

  • Users who have thanked blaze4218 for this post:

    CRC (10-07-2011)

  • #6
    CRC
    CRC is offline
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •