Greetings.
I have a function which watches the content of "price" field and updates the string in "cart" field. In "cart" field, string between | characters gets replaced with whatever is typed in "price". My current function only works once while nothing happens on consecutive changes. I know this is not a problem with the event itself because it works fine if I replace the entire field without regex.
This is the format of the "cart" field and 15 needs to be replaced with content from "price" field: {nicepaypal:cart|15|New in 2010}.
$('price').addEvent('keyup', function() {
var price = $(this).value;
var currentCartValue = $('cart').value;
var oldPrice = String(currentCartValue.match(/\|...\|/));
oldPrice = oldPrice.substring(1, oldPrice.length-1); // ugly but could not get lookaheads for "between" characters to work properly
var newCartValue = currentCartValue.replace(oldPrice, price);
$('cart').value = newCartValue;
});
Another variation does not work either:
newCartValue = currentCartValue.replace(/\|...\|/), '|'+price+'|');
Why is this not working when pressing a key more than once in "price" field. Thank you.
newCartValue = currentCartValue.replace(/\|\d+\.*\d*\|/g, '|'+price+'|');?