View Full Version : another RegExp mystery

01-06-2012, 10:27 PM
I have a situation where there is a string:
which is a tracking method for preserving memory references
in the following string:
In the user interface of the project I am working on; the user
changes the string to:
The object is to revise the memory tracking string to repersent
the changes. should be: (m0|18+m1|(-3))*8

//matches = this.getMemVals()
// getMemVals() searches the string 'm0|18:+:m1|(-3)'
// with regex pattern to find instances of m#|#
var matches = new Array()
matches[0] = 'm0|18';
matches[1] = 'm1|(-3)';
var currentStateSaved = '(18+(-3))*8';
var glbs = '';
for(i = 0; i < matches.length; i++)
var matRegex = new RegExp(matches[i].substring(matches[i].indexOf('|')+1), 'g');
alert('matReg: '+matRegex);
glbs = currentStateSaved.replace(matRegex, matches[i]);
alert('GLBS: '+glbs) // <<< test code

The '18' part of m0|18 is not be found in the revised literal string
Why? (Has '18' been coverted to a number datatype?)

This is the type of problem that drives me up a wall and cost far to
much time trying to flush it out

Thanks of suggestions, advice

01-07-2012, 03:39 AM
I solved the problem, albeit without the use of RegExp:

var glbs = '';
var splits = currentStateSaved;
for(i = 0; i < matches.length; i++)
var matSplit = new Array();
matSplit = splits.split( matches[i].substring(matches[i].indexOf('|') +1) );
splits = matSplit.join(matches[i]);
glbs = splits;
alert('GLBS: '+glbs) // <<< test code

In the process, I think I understand why the RegExp approach was not working.

Thanks to anyone who looked, at least