Reputation: 404
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
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