0

I have a text like this

Right (99.99руб.) OR Left with space (руб. 99.99) OR Right with space (99.99 руб.) OR Left (руб.99.99)

I want to find and change currency symbol with my new symbol.

Here is my code for example

var str = "Right (99.99руб.)",
    symbol = '&&';

console.log(str.replace(/\(\d+\.?\d+\s?(.+)\)|\(([^0-9^\s]+)\s?\d+\.\d+\)/g, symbol))

But it replaces all the text that i have find with regex ("(99.99руб.)") instead of text in captured group ("руб.").

How can i replace only matched text ?

P.S Thanks in advance P.S.S Sorry for my english

Aram810
  • 619
  • 7
  • 21

1 Answers1

0

You could use:

руб\.\ ?(\d[\d.]+)|(\d[\d.]*)\ ?руб

And replace it with $1$2&&, see a demo on regex101.com.


In JavaScript:
var string = 'Right (99.99руб.) OR Left with space (руб. 99.99) OR Right with space (99.99 руб.) OR Left (руб.99.99)';
var re = /руб\.\ ?(\d[\d.]+)|(\d[\d.]*)\ ?руб/g;
string = string.replace(re, '$1$2&&');
alert(string);

See a JS fiddle on jsfiddle.net.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • it must work with any currency not only руб. string can be Left with space (৳ 99.99) – Aram810 Aug 10 '16 at 11:22
  • and you don't need to work with "Right (99.99руб.) OR Left with space (руб. 99.99) OR Right with space (99.99 руб.) OR Left (руб.99.99)" It would be better to write I have a text like one of these "Right (99.99руб.)" or "Left with space (руб. 99.99)" or "Right with space (99.99 руб.)" or "Left (руб.99.99)" – Aram810 Aug 10 '16 at 11:29
  • See https://regex101.com/r/kR2sX3/1, you can add the symbols on the go to the alternation groups. – Wiktor Stribiżew Aug 10 '16 at 12:08
  • @Wiktor Stribiżew it always puts currency symbol at the end of the string – Aram810 Aug 10 '16 at 13:18
  • :) Isn't that what you need? Please update your question with what you need. – Wiktor Stribiżew Aug 10 '16 at 14:00