Regex to Replace Comma to Dot Separator

Regex to replace comma to dot separator

You can use capture groups and reference them in the replacement:

gsub("(\\d+),(\\d+)", "\\1.\\2", "Today I bought apples, oranges for 3,55 dollars")

# [1] "Today I bought apples, oranges for 3.55 dollars"

Replace comma with period in string only if it is a decimal number

Capture a digit, match a comma, lookahead for another digit, and replace with the captured group plus a period:

var string = "This is the number 9,5 but problems is if we have e.g. 10, 11."console.log(  string.replace(/(\d),(?=\d)/g, '$1.'));

Replace ,(comma) by .(dot) and .(dot) by ,(comma)

Use replace with callback function which will replace , by . and . by ,. The returned value from the function will be used to replace the matched value.

var mystring = "1,23,45,448.00";
mystring = mystring.replace(/[,.]/g, function (m) { // m is the match found in the string // If `,` is matched return `.`, if `.` matched return `,` return m === ',' ? '.' : ',';});
//ES6mystring = mystring.replace(/[,.]/g, m => (m === ',' ? '.' : ','))

How do I replace commas with dot in the quoted sub-string only?

You can do that using preg_replace

$string = preg_replace('/("\d+),(\d+")/','$1.$2',$string);

How to replace comma with a dot in the number (or any replacement)

As replace() creates/returns a new string rather than modifying the original (tt), you need to set the variable (tt) equal to the new string returned from the replace function.

tt = tt.replace(/,/g, '.')


Replacing comma's by dots in floats using regular expressions

just replace (\d),(\d) with \1.\2

don't have notepad++. in eclipse it worked.

Replacing comma by dot only in numbers in a texteditor using search and replace function

EditPlus does not support lookbehinds, so you need to use groups.


Substitution: \1,\2

  • () Capturing group
  • \d matches a digit (equal to [0-9])
  • \1 \2 Capturing group 1. and group 2.

Sample Image

Related Topics

Leave a reply