最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript regex string replace works only once - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Apr 6, 2011 at 21:19 alex-techalex-tech 4073 silver badges14 bronze badges 2
  • Moreover, you have to be sure that you always have only 3 characters between |. Assuming that price should always be a number (maybe a float), couldn't you change that to newCartValue = currentCartValue.replace(/\|\d+\.*\d*\|/g, '|'+price+'|');? – MPękalski Commented Apr 6, 2011 at 21:25
  • This was the actual problem: my regex expression was flawed and I changed ... to (.*) Can you post as a main answer so I can upvote you? – alex-tech Commented Apr 6, 2011 at 21:53
Add a ment  | 

3 Answers 3

Reset to default 5

You need to add the g (Global) flag to the regex.

newCartValue = currentCartValue.replace(/\|...\|/g, '|'+price+'|');

You have to be sure that you always have only 3 characters between |.

Assuming that price should always be a number (maybe a float), couldn't you change that to

newCartValue = currentCartValue.replace(/\|\d+\.*\d*\|/g, '|'+price+'|');?

If you do not want to check for the number try this

newCartValue = currentCartValue.replace(/\|.*?\|/g, '|'+price+'|');

The ? after .* is crutial, as it makes sure it matches all characters between |, and no | will be found inside.

String.replace will only replace the first instance by default. This can be changed by use of the global (g) flag when using a regex. See use of the g flag below in the first example.

// replace globally
var str1 = "A man is a man is a man."
str1 = str1.replace(/man/g, "woman");
alert(str1);

// replaced only once
var str2 = "A dog is a dog is a dog."
str2 = str2.replace("dog", "cat");
alert(str2);
发布评论

评论列表(0)

  1. 暂无评论