-1

I have read How do you pass a variable to a Regular Expression javascript

I'm looking to create a regular expression to get and replace a value with a variable..

 section = 'abc';
 reg = new RegExp('\[' + section + '\]\[\d+\]','g');
 num = duplicate.replace(reg,"$1++");

where $1 = \d+ +1

and... without increment... it doesn't work...

it returns something like:

[abc]$1

Any idea?

Community
  • 1
  • 1
Jean-philippe Emond
  • 1,457
  • 1
  • 15
  • 29

3 Answers3

1

I think you need to read up more on Regular Expressions. Your current regular expression comes out to:

/[abc][d+]/g

Which will match an "a" "b" or "c", followed by a "d" or "+", like: ad or c+ or bd or even zebra++ etc.

A great resource to get started is: http://www.regular-expressions.info/javascript.html

rgthree
  • 6,849
  • 15
  • 20
  • Incorrect. This is basic backslash behaviour. Backslash followed by anything that is does not make it an escape sequence, will remain literal. – Niet the Dark Absol Dec 03 '13 at 20:32
  • It's even weirder than that, `a+` doesn't pass. I guess because the `+` isn't escaped. Either way, it's a pretty muddled regex. – Justin Morgan Dec 03 '13 at 20:32
  • @NiettheDarkAbsol - It looked that way to me too, but according to the Chrome console, rgthree is right. Other than the `+` issue I explained above. Try it: http://jsfiddle.net/eTh4A/ – Justin Morgan Dec 03 '13 at 20:33
  • Hey guys. Yeah, looks like you've already figured it out, but the backslashes escape within the string here, not within the RegExp itself. You would need two backslashes to do that `'\\[abc\\]'` would be `/\[abc\]/` once parsed. Also, @JustinMorgan, "a+" passes for me. If you run `new RegExp('\[' +'abc' + '\]\[\d+\]','g').test('a+')` in Chrome console, it should return `true`. – rgthree Dec 03 '13 at 20:54
  • You're right, it does pass. I don't know what I did differently the first time, but I must have mistyped something. That makes a lot more sense. – Justin Morgan Dec 12 '13 at 15:47
1

Your regex is on the right track, however to perform any kind of operation you must use a replacement callback:

section = "abc";
reg = new RegExp("(\\["+section+"\\]\\[)(\\d+)(\\])","g");
num = duplicate.replace(reg,function(_,before,number,after) {
    return before + (parseInt(number,10)+1) + after;
});
Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
0

I see at least two problems.

The \ character has a special meaning in JavaScript strings. It is used to escape special characters in the string. For example: \n is a new line, and \r is a carriage return. You can also escape quotes and apostrophes to include them in your string: "This isn't a normally \"quoted\" string... It has actual \" characters inside the string as well as delimiting it."

The second problem is that, in order to use a backreference ($1, $2, etc.) you must provide a capturing group in your pattern (the regex needs to know what to backreference). Try changing your pattern to:

'\\[' + section + '\\]\\[(\\d+)\\]'

Note the double-backslashes. This escapes the backslash character itself, allowing it to be a literal \ in a string. Also note the use of ( and ) (the capturing group). This tells the regex what to capture for $1.

After the regex is instantiated, with section === 'abc':

new RegExp('\\[' + section + '\\]\\[(\\d+)\\]', 'g');

Your pattern is now:

/\[abc\]\[(\d+)\]/g

And your .replace will return \d+++ (where \d+ is the captured digits from the input string).

Demo: http://jsfiddle.net/U46yx/

pete
  • 21,669
  • 3
  • 34
  • 49