1
var prefix = 'pre-',
    number = 1,
    combined = prefix + number,
    prefixRemoved = combined.replace('/' + prefix + '/g', '');
console.debug(prefixRemoved);

How do I remove prefix from combined? I'm still getting pre-1 as a result.

1

5 Answers 5

1

You can just pass a string to replace() like this:

prefixRemoved = combined.replace(prefix, '');

or if you need parameters on the regular expression, you can create a regexp from a string like this:

prefixRemoved = combined.replace(new RegExp(prefix, "g"), '');

or, you can create the regex object first:

var re = new RegExp(prefix, "g");
prefixRemoved = combined.replace(re, '');
Sign up to request clarification or add additional context in comments.

Comments

1

You need to create the RegExp object:

var prefix = 'pre-',
    number = 1,
    combined = prefix + number,
    prefixRemoved = combined.replace(new RegExp(prefix, 'g'), '');
console.debug(prefixRemoved);

Example


Why that is happening:

// this syntax without quotes is shorthand for creating a RegExp object
typeof /aaaa/; // object
/a/ instanceof RegExp; // true

// since String.replace can take a string or a RegExp, it has to assume that any string is just a string 
typeof "/aaaa/"; // string

Comments

0

You don't need a regex for this. Simply use the string as is:

var prefix = 'pre-',
    number = 1,
    combined = prefix + number,
    prefixRemoved = combined.replace(prefix, '');
console.debug(prefixRemoved);

6 Comments

+1, but I guess, techically it can be simplified to console.log(number)
there is a difference between /prefix/g and prefix, one replaces one match, the other replaces all matches. In this instance it might not be an issue, but the general question is how to use a variable within a RegEx.
@zzzzBov - I was simply trying to help the OP solve his problem. A lot of people jump first to regex when they have a problem. But really, that should be the last tool you use, after everything else in your toolkit has failed you...
a global variable replace is very common. I'd rather have people understand how to make a simple RegEx to do what they need rather than avoid RegEx out of fear of the unknown.
@zzzzBov - He specifically asked to remove a prefix, and the best way to do that is the way I've shown him...
|
0

There's really no need for using regular expressions since you are merely performing string replacement.

prefixRemoved = combined.replace(prefix, '');

See it in action: http://jsfiddle.net/jjYC8/

3 Comments

this is incorrect, it will only replace the first occurrence of prefix. Using /prefix/g will replace all occurrences.
True. But look at the example code: pre-1. So there's no need for the global replacement. Thanks for the down vote though.
+1 there is no need to vote this down @zzzzBov. The OP never said he needed to replace more than one instance.
0

You need prefixRemoved = combined.replace(prefix, '');

var prefix = 'pre-',
    number = 1,
    combined = prefix + number,
    prefixRemoved = combined.replace(prefix, '');
console.debug(prefixRemoved);

Just call the variable directly.

Example: http://jsfiddle.net/jaffc/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.