views:

295

answers:

4

How can I replace the number inside the brackets for any strings not matching the word "Field". So the number inside 'SomethingElse' and 'SomethingMore' could be replaced to a new value, but any bracketed value to the right side of the term 'Field' would not be touched. Note, the word "Field" will always stay the same, so it can be referenced as a magic string in the regex.

Field[50].SomethingElse[30]
Field[50].SomethingMore[30]

Thanks. PS. Using JavaScript.

+1  A: 

Try

str.replace(/(Field\[[^\]]*\]\.[^\[]*)\[(.*)\]/g, "$1["+value+"]");
Eldar Djafarov
Thanks Eldar, sorry for the very late accept.
GONeale
A: 

You could use "eval" and string manipulation, but if you have to do it that way, you're doing it wrong.

Storing the number "30" to a variable allows it to be manipulated as you require, then just access "SomethingElse" and "SomethingMore" as follows:

var n = 30;
// conditional logic;

Field[50].SomethingElse[n]
Field[50].SomethingMore[n]

Regards, Joshua

Joshua
+2  A: 
str.replace(/\b((?!Field\[)\w+)\[\d+\]/g, '$1[' + repl + ']');
Alan Moore
A: 

str.replace(/(?<!Field)[([\d]*)]]/g, '$1[' + newnumber + ']'); Hope this helps