Link to home
Create AccountLog in
Avatar of Badotz
BadotzFlag for United States of America

asked on

Creating a RegExp using variable substitution

I want to create a RegExp instead of using eval as in the attached function.

No matter what I have tried, everything fails miserably, either not modifing the input string or whiing about improper syntax.

I've been up all night working, and this little hurdle has me stumped. I won't be able to sleep until it is resolved.

The eval in the function works, but I <strong>dislike</strong> that particular verb.

All help is appreciated!


function pretty(str, len, sep) {
    str = str || "000102030405060708090a0b0c0d0e0f";
    len = len || 2;
    sep = sep || " ";
    
    return eval ( "str.replace(/(.{" + len + "})/g,\"$1" + (sep || " ") + "\")" );
}

Open in new window

Avatar of Proculopsis
Proculopsis


Here's an example of a dynamic Regular Expression, the trick is to use new RegExp:

String.prototype.words = extendStringWords;
function extendStringWords( skip, count )
{
  var result = "";
  var expression = "^(?:\\b\\w+\\W*){";
  expression += skip;
  expression += "}((?:\\b\\w+\\W*){0,";
  expression += count;
  expression += "})(?:\\b\\w+\\W*)*$";

  var re = new RegExp( expression );

  if ( re.exec( this ) != null ) {
    result = this.replace( re, "$1" );
  }

  return result;
};

Open in new window

Avatar of Badotz

ASKER

Right, I've read all about "new RegExp", and the trivial examples.

I cannot migrate my eval version to use it. That is my problem.
ASKER CERTIFIED SOLUTION
Avatar of Proculopsis
Proculopsis

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Badotz

ASKER

Hardly - see the results below.


09undefined74undefinedCDundefined65undefined4Cundefined91undefined9Aundefined16undefined80undefined40undefined25undefinedABundefined40undefinedA0undefinedB6undefined10undefined92undefined75undefinedE9undefined6FundefinedD0undefined08undefinedA3undefined40undefined32undefined22undefined5AundefinedC1undefined90undefined23undefined01undefined2Bundefined68undefinedADundefined29undefined45undefined52undefined3BundefinedB9undefinedB4undefined02undefined15undefined2Aundefined7AundefinedF1undefined86undefinedE8undefined04undefined45undefinedA0undefined19undefinedAAundefined32undefined64undefinedBAundefinedABundefined92undefined

Open in new window

Avatar of Badotz

ASKER

But this works, thanks.


function pretty(str, len, sep) {
	var re = new RegExp( "(.{" + len + "})", "g" );

	return str.replace( re, "$1" + (sep || " ") );
}

Open in new window

Avatar of Badotz

ASKER

Thanks! No I can get some shut-eye!

add...


len = ( "undefined".indexOf(typeof(len)) != 0 )? len : 2;
sep = ( "undefined".indexOf(typeof(sep)) != 0 )? sep : " ";

Avatar of Badotz

ASKER

That's way too verbose - I'll use the expressions (len || 16) and (sep || " ") for the default value, thanks.

This is for "pretty-printing" long hex and/or binary strings. It is used in the context of a JavaScript library and is not a public function. Therefore, error-checking can be kept to a minimum.

Thanks again.
Avatar of Badotz

ASKER

The final function:


function pretty(str, len, sep) {
    return str.replace( new RegExp( "(.{" + len + "})", "g" ), "$1" + (sep || " ") );
}

Open in new window