Shingala94
Shingala94

Reputation: 404

Javascript replace by regex but only the first character

I Want to replace spaces in a string in javascript. But only if there is a price behind it.

Example:

var before = 'Porto Rood / Wit 4,00';
var after = 'Porto Rood / Wit;4,00';

The regex I use is \s\d+,\d{2}

In javascript is there a way to replace only the first character of a regex match ?

Upvotes: 1

Views: 381

Answers (1)

kind user
kind user

Reputation: 41913

You can use positive lookahead to match only the whitespace before the price.

var before = 'Porto Rood / Wit 4,00',
    after = before.replace(/\s(?=\d+,\d{2})/, ';');
    console.log(after);

Upvotes: 2

Related Questions