Mateusz Urbański
Mateusz Urbański

Reputation: 7872

Javascript replace regular expression

In my application I use the javascript replace method with following regex:

replace(/\€/g,'$')

This code find € character and change it to $. But now I want to this code replace € and £ to $ character. How can I do that?

Upvotes: 0

Views: 76

Answers (3)

hwnd
hwnd

Reputation: 70742

You can use a character class to define one of several characters, as well they dont need to be escaped.

'€ and £'.replace(/[£€]/g, '$'); //=> '$ and $'

Upvotes: 1

Sainath Motlakunta
Sainath Motlakunta

Reputation: 945

use this replace(/[\€\£]/g,'$') You can mention 2 or more characters in square brackets to match them individually.

Upvotes: 1

the6p4c
the6p4c

Reputation: 664

Use the OR operator to combine the pound and euro symbols (of which you do not need to escape). The resulting regex would be:

/€|£/g

Upvotes: 1

Related Questions