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 5 of 5
  1. #1
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post

    Complex Regular Expression

    I've been trying to figure out how to write a regular expression to match a regular expression literal, but am having issues with false positives. Here's what I have currently:

    /^(?:\/((?:\\.|\[(?:\\.|[^\]])*\]|[^\/])+)\/([gimy]*))/i

    and here is an example of a string it gives a false positive on:

    / 2;
    /
    /comment
    var b = /\n/g;


    Any thoughts on what I may be doing wrong with this regular expression?
    (FYI: The leading portions of the input string are removed as matches are found, which is why the division operator is now the first character in the input string).

    As always, any insight is appreciated

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    It depends on what you are doing, but I use this expression when I want to sequester regular expressions from a code formatting routine-
    var Rx= /(^|[\(\=\:\[,] *)((\/([^\/]+\/))[gim]*)/g;

    To return the reg exps in a string, like a file's text or json output, you could do something like this-

    Code:
    var getRx= function(str){
    	str= str.split(/\r?\n/);
    	var A= [], Rx, i= 0, s;
    	var Rx= /(^|[\(\=\:\[,] *)((\/([^\/]+\/))[gim]*)/g;
    	while(str.length){
    		s= str.shift();		
    		var  M;
    		while(s && (M= Rx.exec(s))!= null){			
    			A.push(M[2]);
    		}
    	}
    	return A;
    }
    Last edited by mrhoo; 01-16-2009 at 04:11 PM.

  • #3
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    hmm, doesn't look like that works either:

    http://regexpal.com/

    (^|[\(\=\:\[,] *)((\/([^\/]+\/))[gim]*)
    ------------------------------------------------------------------------
    / 2;
    //comment
    var b = /\n/g;

  • #4
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    Some more examples of failed matches and what I'm looking for in bold:

    var b = /\n/g;
    var re = /\n///comment
    var re2 = /\//
    var NAME = /^[a-z_$][\w$]*/i,
    //comment

  • #5
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    Problem solved:

    /^\/((?![*\/])(?:\\\/|[^\n\r\u2028\u2029\/])+[^\n\r\u2028\u2029\\])\/([gimy]*)/

    credit: http://www.webdeveloper.com/forum/sh...94&postcount=8


  •  

    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
    •