限制搜索和替换?

Restrict search and replace?

我有使用 replace() 的这个函数,它找到值“0 €”并替换为 'no price'。它工作正常,但问题是它匹配 这个 '0 欧元' 还有这个 '10 欧元'。

我只需要它来匹配“0 €”而不是“10 €”。

js:

$('.field-name').each(function() {
    var text = $(this).text();
    $(this).text(text.replace('0 €', 'no price')); 
});

你可以这样做:

$(this).text(text.replace(/(^|\D)0\s*€/g, 'no price'));

这将替换“0 $”,但仅限于字符串的开头或非数字的内容之后。