User input regular expression to regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scriptic
    New Member
    • Jan 2010
    • 6

    User input regular expression to regex

    I want to be able to enter a regular expression in a text field and use it as a regex to search the text from a textbox.

    This code says reg.compile is not a function, but the same regex entered in code works. I think it is a string conversion problem.

    Thanks for your help.

    Code:
    var feed=document.forms[0].feedback.value;
    	var	reg =document.forms[0].pattern.value + ";";
    
    	reg.compile(reg);
    
    	var	aray = reg.exec(feed);
  • larztheloser
    New Member
    • Jan 2010
    • 86

    #2
    Use that method that lets you run code as a string:

    Code:
    eval("reg=/"+document.forms[0].pattern.value+"/;");

    Comment

    • Scriptic
      New Member
      • Jan 2010
      • 6

      #3
      Thanks Larz, but that wasn't the problem. The eval statement didn't change anything and adding the slashes just gave me an extra slash on each end of the regular expression I typed in.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        you could use the regExp constructor for that:
        Code:
        // suppose s is your pattern
        var s = 'foo';
        
        // construct the regExp - note the switches could even be
        // passed here
        var re = new RegExp(s, 'gi');
        
        // use the regExp to test a string
        alert(re.test('foobar'));
        kind regards

        PS: btw. eval is never needed except when working with json-data ... so avoid the eval whenever possible :)

        Comment

        • Scriptic
          New Member
          • Jan 2010
          • 6

          #5
          Thanks gits. The problem with RegExp is that it takes regular text combined with arguments and turns them into a regex. I want to enter a regular expression with arguments and slashes into a text box, read it using a javascript and search text from another text box.
          The problem is even though what I enter looks like a regex when I print it to screen, it doesn't work with compile() or exec(). There must be some extra character that is preventing those functions from using my input.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            where is the problem? ... just handle the string before passing it to the regExp constructor - which accepts 2 params as strings - so that would be your handled inputs. without that constructor you will not be able to create a regExp dynamically ...

            Comment

            • Scriptic
              New Member
              • Jan 2010
              • 6

              #7
              That could be one option.Why does this work, but when the value is from a text box it doesn't. That would be good to know.

              Code:
              var str="Hello world!";
              //look for "Hello"
              var patt=/Hello/g;
              var result=patt.exec(str);
              Thanks again.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                it's a matter of using the literals ... as you see it is written directly without any parameters that could be passed. The literals are just shorthand for using the constructor for example:
                Code:
                var a = [];
                is equivalent to:
                Code:
                var a = new Array;
                the same goes for the regExp ...where you could pass parameters to construct a new RegExp. So basically there is no difference between the usage of the literals or the constructor except the when the literals could be used they should be used. As you see they are much shorter and so the javascript-interpreter has less to evaluate and therefor using the literals is faster ... so use it when you can ... but in the current case ... you cannot ... to create a regExp dynamically ... you have to use the objects constructor-method.

                kind regards

                Comment

                • Scriptic
                  New Member
                  • Jan 2010
                  • 6

                  #9
                  Thanks for the explanation

                  I've resolved to do the string processing that you suggested in a previous post. I've researched this quite a bit and not found better solution.

                  Here is what I have so far. It seems to work except for some reason when I search for /Hell/g in "Hello, world! Hell-o-world!" it only finds one instance.

                  Code:
                  var feed=document.forms[0].feedback.value;
                  
                  var	inputstring =document.forms[0].pattern.value;
                  
                  var match = inputstring.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
                  
                  var regex = new RegExp([match[1]], match[2]);
                  
                  var	aray = regex.exec(feed);
                  If I print regex to screen it looks like /Hell/g.
                  In case anyone is interested.

                  Thanks for all your help.

                  Comment

                  • Scriptic
                    New Member
                    • Jan 2010
                    • 6

                    #10
                    Never mind.

                    /(Hell)/g finds all instances in "Hello, world! Hell-o-world!" and remembers them.

                    Comment

                    Working...