In JavaScript, '^\D*(\d+) рублей.*$' Is a string, not a regular expression. Regular expressions are enclosed in /.../ . '...' is a string, not a regular expression, that is, your code searches for the string '^\D*(\d+) рублей.*$' .
Farther. Your regular expression starts with a line start character ( ^ ) and ends with a line end character ( $ ). That is, the whole string is necessarily compared. If you do not check the entire line for correctness, but look for something inside the line, do not use ^ and $ , they are superfluous.
Plus, remove "anything" ( .* ) At the end, because in this case, the regular expression will capture everything that remains until the end - including the following numeric dates, but we do not need it.
We get the following regular expression: /(\d+) рублей/
In principle, after the digit there can be not only a space, but also, for example, tabulation. So it is better to replace on \s . And there may be several space characters, so it is better to put + ( \s+ ).
Perhaps it makes sense not only "rubles", but also "rubles", and "ruble", and "rubles." /(\d+)\s+руб(лей|ля|ль|\.)/
True, it does not include pennies. Suppose that pennies go right after rubles (in general, this is not necessarily true - see your input, but suppose). Also suppose that pennies are not found without rubles:
/(\d+)\s+руб(лей|ля|ль|\.)(\s*(\d+)\s+коп(еек|йки|йка|\.))?/
I put a penny in the brackets and set ? after the parentheses to indicate that it is not necessary to indicate that pennies.
To replace all values, you can use the replace function, passing a function to it — something like this:
исходнаяСторока.replace(/регулярка/g, function (match, скобки1, скобки2, скобки3, ...) { return 'чем заменить'; });
g after the regular expression means that it is necessary to replace not only the first entry, but all entries.
For an example, here’s a function that converts rubles to dollars:
function rublesToDollars(str, course) { return str.replace( /(\d+)\s+руб(лей|ля|ль|\.)(\s*(\d+)\s+коп(еек|йки|йка|\.))?/g, function (match, rubles, rub_ending, copecks_match, copecks, copecks_ending) { var rubles = parseInt(rubles); if (copecks) { rubles += parseInt(copecks) / 100; } var dollars = rubles / course; return dollars.toFixed(2) + '$'; }); }
Here is an example of use:
rublesToDollars("Инна заказала пиццу Tut.by " + "за 8 рублей 90 копеек, а я — салат за 5 рублей " + "50 копеек", 1.9031)
Result:
"Инна заказала пиццу Tut.by за 4.68$, а я — салат за 2.89$"
Next you need to apply it to the whole text. $("body").text() doesn’t fit exactly, since this is a data loss conversion: you lose all the tags inside <body> , you basically make unformatted text from HTML.
The function above can work only with text, but not with tags. If you know that the text with the amounts will be stored only in certain elements (for example, in the descriptions of products), then the easiest way is to replace the text only in these elements.
If you need something more universal, then it is better to use ready-made plug-ins for replacement in the whole code. For example, https://github.com/padolsey/findAndReplaceDOMText